Select to view content in your preferred language

How to "Activate" a MapFrame in C#

719
2
Jump to solution
09-05-2022 01:12 AM
pln_arxit
New Contributor

When interacting with ArcGIS Pro UI, it is possible to "activate" a MapFrame element (Right click → Activate) in the displayed Layout. Is it possible to do that with the SDK in C# ?
There is no such method in the MapFrame class or in the Layout class (with ArcGIS Pro 2.9).

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
CharlesMacleod
Esri Regular Contributor

This has been added to the API on LayoutView class at the next release 3.1.

In the meantime, u can use this code snippet as a basis for a workaround.

4 conditions must be true in order to activate a map frame:

1. You must be on the UI thread

2. The Layout view (containing the relevant map frame) must be the active view

3. The map frame to be activated must be the LayoutView.Active.ActiveMapFrame

4. The LayoutView.Active.ActiveMapFrame.Map must not be null (meaning it has an empty map frame)

If these conditions are met you can invoke the "esri_layouts_activateMap" command which will activate the LayoutView.Active.ActiveMapFrame.

 

internal class ActivateMapFrameBtn: Button {

 protected override void OnClick()
 {
  //1. must be on the GUI - dont switch to QueuedTask

  //2. Layout view must be active!
  if (LayoutView.Active == null)
    return; 

  //3. LayoutView.Active.ActiveMapFrame is the frame that will
  //   be activated
  var map_frameName = "Map Frame";
  if (LayoutView.Active.ActiveMapFrame == null ||
     LayoutView.Active.ActiveMapFrame.Name != map_frameName) {

     //simply select it to make it the active one
     var selFrame = LayoutView.Active.Layout.GetElementsAsFlattenedList()
        .OfType<MapFrame>().FirstOrDefault(mf => mf.Name == map_frameName);
     if (selFrame == null)
       return; //not found

     LayoutView.Active.Layout.SelectElement(selFrame);
   }

   //4. Does the ActiveMapFrame have a valid map?
   if (LayoutView.Active.ActiveMapFrame?.Map == null)
     return; //invalid map frame - probably empty

   //esri_layouts_activateMap - activate
   //esri_layouts_deactivateMap - deactivate

   //Do the activation
   var plugin = 
       FrameworkApplication.GetPlugInWrapper("esri_layouts_activateMap") 
                                         as System.Windows.Input.ICommand;
   if (plugin.CanExecute(null))
     plugin.Execute(null);
 }
}

 

 

 

 

View solution in original post

2 Replies
CharlesMacleod
Esri Regular Contributor

This has been added to the API on LayoutView class at the next release 3.1.

In the meantime, u can use this code snippet as a basis for a workaround.

4 conditions must be true in order to activate a map frame:

1. You must be on the UI thread

2. The Layout view (containing the relevant map frame) must be the active view

3. The map frame to be activated must be the LayoutView.Active.ActiveMapFrame

4. The LayoutView.Active.ActiveMapFrame.Map must not be null (meaning it has an empty map frame)

If these conditions are met you can invoke the "esri_layouts_activateMap" command which will activate the LayoutView.Active.ActiveMapFrame.

 

internal class ActivateMapFrameBtn: Button {

 protected override void OnClick()
 {
  //1. must be on the GUI - dont switch to QueuedTask

  //2. Layout view must be active!
  if (LayoutView.Active == null)
    return; 

  //3. LayoutView.Active.ActiveMapFrame is the frame that will
  //   be activated
  var map_frameName = "Map Frame";
  if (LayoutView.Active.ActiveMapFrame == null ||
     LayoutView.Active.ActiveMapFrame.Name != map_frameName) {

     //simply select it to make it the active one
     var selFrame = LayoutView.Active.Layout.GetElementsAsFlattenedList()
        .OfType<MapFrame>().FirstOrDefault(mf => mf.Name == map_frameName);
     if (selFrame == null)
       return; //not found

     LayoutView.Active.Layout.SelectElement(selFrame);
   }

   //4. Does the ActiveMapFrame have a valid map?
   if (LayoutView.Active.ActiveMapFrame?.Map == null)
     return; //invalid map frame - probably empty

   //esri_layouts_activateMap - activate
   //esri_layouts_deactivateMap - deactivate

   //Do the activation
   var plugin = 
       FrameworkApplication.GetPlugInWrapper("esri_layouts_activateMap") 
                                         as System.Windows.Input.ICommand;
   if (plugin.CanExecute(null))
     plugin.Execute(null);
 }
}

 

 

 

 

pln_arxit
New Contributor

That's it! Thank you so much!

0 Kudos