//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;
});
}
}