How to Set name for Map Frame while reusing the Arcgis pro tool.

221
2
10-26-2021 12:34 AM
by Anonymous User
Not applicable

Hi,

I need to add functionality in the layout to allow the user to add multiple Map Frames as needed. I'm using the ArcGIS Pro tool functionality (Tool ID: esri_layouts_reshapeMapFrameRectangleTool) for this.

Below is the code snippet I have used:

ShabinaBano_0-1635233115617.png

-----------------------------------------------------------------------------------------------

//reusing the pro tool
public ICommand SelectLinearBandCommand =>

new ArcGIS.Desktop.Framework.RelayCommand(() =>
{
var SelectLinearBandcmd = FrameworkApplication.GetPlugInWrapper("esri_layouts_newMapFrameRectangleTool") as ICommand;

SelectLinearBandcmd.Execute(null);

LinearBandButton(layout, map, mfElm);
},
() => (FrameworkApplication.GetPlugInWrapper("esri_layouts_newMapFrameRectangleTool") as ICommand).CanExecute(null)
);


//made function "LinearBandButton" and called that into above command
private async void LinearBandButton(Layout layout, Map map, MapFrame mfElm)
{
await QueuedTask.Run(() =>
{
layout = LayoutFactory.Instance.CreateLayout(LayoutConstants.Width, LayoutConstants.Height, LayoutConstants.Units);
layout.SetName(LayoutName);

ArcGIS.Core.Geometry.Envelope env = EnvelopeBuilder.CreateEnvelope(LayoutConstants.xMin, LayoutConstants.yMin, LayoutConstants.xMax, LayoutConstants.yMax);
MapGlobal.Instance.envelope = env;

MapProjectItem mapPrjItem = Project.Current.GetItems<MapProjectItem>().FirstOrDefault(item => item.Name.Equals(LayoutConstants.MapName));

map = mapPrjItem.GetMap();
map.SetSpatialReference(sptlRef);
mfElm = LayoutElementFactory.Instance.CreateMapFrame(layout, env, map);
//When I add the Map Frame to the Layout, I want it to be called "Linear Band."
mfElm.SetName("Linear Band");
});
}

-------------------------------------------------------------------------

Issue: The issue is that I need to set name('Linear Band') after the Map Frame is added to the layout, but the setName functionality is currently running before that, so the name does not change from "Map Frame" to "Linear Band." When we add the Map Frame to the layout, the default name is "Map Frame," which I want to change to "Linear Band."

 

Could anyone please assist me in resolving this issue? It would be great if you could provide a code snippet on how to do so.

 

Thanks in advance.

0 Kudos
2 Replies
CharlesMacleod
Esri Regular Contributor

this is probably a timing issue - the name change being applied before the UI refresh (related to the new element being added) has completed. Separate the name change in to its own QueuedTask and u should get the result u r looking for.

Like so:

private async void LinearBandButton(Layout layout, Map map, MapFrame mfElm)
{

var mfElm = await QueuedTask.Run(() =>
{
layout = LayoutFactory.Instance.CreateLayout(...);
   ...
return LayoutElementFactory.Instance.CreateMapFrame(layout, env, map);
});

//separate queuedtask
QueuedTask.Run(()=> {
//When I add the Map Frame to the Layout, I want it to be called "Linear Band."
mfElm.SetName("Linear Band");
});
}

 

0 Kudos
by Anonymous User
Not applicable

public ICommand SelectLinearBandCommand =>
new ArcGIS.Desktop.Framework.RelayCommand(async () =>
{

var SelectToolcmd = FrameworkApplication.GetPlugInWrapper("esri_layouts_newMapFrameRectangleTool") as ICommand;
SelectToolcmd.Execute(null);


//Added the below event and it pretty much worked
ArcGIS.Desktop.Layouts.Events.ElementAddedEvent.Subscribe(LinearBandButton);
TreeListDataController.DisableThreadingProblemsDetection = true;

},

 

() => (FrameworkApplication.GetPlugInWrapper("esri_layouts_newMapFrameRectangleTool") as ICommand).CanExecute(null)
);

 

 

 

 

Thanks

0 Kudos