ArcGIS Pro 3.1 API Reference Guide
ArcGIS.Desktop.Layouts Namespace / MapSurround Class / SetMapFrame Method
MapFrame
Example

In This Topic
    SetMapFrame Method
    In This Topic
    Sets a MapFrame to the MapSurround. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public void SetMapFrame( 
       MapFrame mapFrame
    )
    Public Sub SetMapFrame( _
       ByVal mapFrame As MapFrame _
    ) 

    Parameters

    mapFrame
    MapFrame
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Example
    MapSurroundExample
    //Added references
    using ArcGIS.Desktop.Core;                         //Project
    using ArcGIS.Desktop.Layouts;                      //Layout class
    using ArcGIS.Desktop.Framework.Threading.Tasks;    //QueuedTask
    
    public class MapSurroundExample
    {
      public static Task<bool> UpdateMapSurroundAsync(string LayoutName, string SBName, string MFName)
      {
        //Reference a layoutitem in a project by name
        LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals(LayoutName));
        if (layoutItem == null)
          return Task.FromResult(false);
    
        return QueuedTask.Run<bool>(() =>
        {
                //Reference and load the layout associated with the layout item
                Layout lyt = layoutItem.GetLayout();
    
                //Reference a scale bar element by name
                MapSurround scaleBar = lyt.FindElement(SBName) as MapSurround;
          if (scaleBar == null)
            return false;
    
                //Reference a map frame element by name
                MapFrame mf = lyt.FindElement(MFName) as MapFrame;
          if (mf == null)
            return false;
    
                //Set the scale bar to the newly referenced map frame
                scaleBar.SetMapFrame(mf);
          return true;
        });
      }
    }
    TableFrame_CreateNew
    //Create a new table frame on the active layout.
    
    Layout layout = LayoutView.Active.Layout;
    
    //Perform on the worker thread
    await QueuedTask.Run(() =>
    {
      //Build 2D envelope geometry
      Coordinate2D rec_ll = new Coordinate2D(1.0, 3.5);
      Coordinate2D rec_ur = new Coordinate2D(7.5, 4.5);
      //At 2.x - Envelope rec_env = EnvelopeBuilder.CreateEnvelope(rec_ll, rec_ur);
      Envelope rec_env = EnvelopeBuilderEx.CreateEnvelope(rec_ll, rec_ur);
    
      //Reference map frame
      MapFrame mf = layout.FindElement("Map Frame") as MapFrame;
    
      //Reference layer
      Map m = mf.Map;
      FeatureLayer lyr = m.FindLayers("GreatLakes").First() as FeatureLayer;
    
      //Build fields list
      var fields = new[] { "NAME", "Shape_Area", "Shape_Length" };
    
      //Construct the table frame
      //At 2.x - TableFrame tabFrame = LayoutElementFactory.Instance.CreateTableFrame(layout, rec_env, mf, lyr, fields);
      var surroundInfo = new TableFrameInfo()
      {
        FieldNames = fields,
        MapFrameName = mf.Name,
        MapMemberUri = lyr.URI
      };
      var tabFrame = ElementFactory.Instance.CreateMapSurroundElement(layout, rec_env, surroundInfo) as TableFrame;
    });
    MapSurround_SetMapFrame
    await QueuedTask.Run(() =>
    {
      LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>()
                  .FirstOrDefault(item => item.Name.Equals("Layout Name"));
      Layout lyt = layoutItem.GetLayout();
      MapFrame mf = lyt.FindElement("Map1 Map Frame") as MapFrame;
      MapSurround ms = lyt.FindElement("Scale Bar") as MapSurround;
    
      ms.SetMapFrame(mf);
    });
    Update a map surround
    //Update a map surround.
    
    //Perform on the worker thread
    QueuedTask.Run(() =>
    {
      // Reference and load the layout associated with the layout item
      Layout layout = layoutItem.GetLayout();
      if (layout != null)
      {
        // Reference a scale bar element by name
        MapSurround scaleBar = layout.FindElement("MyScaleBar") as MapSurround;
    
        // Reference a map frame element by name
        MapFrame mf = layout.FindElement("MyMapFrame") as MapFrame;
    
        if ((scaleBar != null) && (mf != null))
          //Set the scale bar to the newly referenced map frame
          scaleBar.SetMapFrame(mf);
      }
    });
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3.0 or higher.
    See Also