ArcGIS Pro hangs on layout added by code and when map frame is activated

764
3
02-09-2021 09:02 AM
MyriamFischer
New Contributor II

I'm working with ArcGIS Pro 2.6 and when I add a layout by code and activate the mapframe in the layout, it is not responding any longer. The ribbons are grayed out and the Layout cannot be reactivated. When I import the same layout from the catalog pane, it is working correctly. There is just one default layer in the map on the layout. When I wait long enough I can close the map frame and return to the layout. To activate the map frame now is not a problem any more.

Code for adding the layout:

var layout  = await QueuedTask.Run(() => GetLayout(selectedLayoutPath, name));

private Layout GetLayout(string path, string layoutname)

{

var pathDef = string.Concat(path, @"\", layoutname, @".pagx");

var layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals(layoutname));
if (layoutItem != null)
{
Project.Current.RemoveItem(layoutItem);
}

var pagx = (IProjectItem)ItemFactory.Instance.Create(pathDef);
Project.Current.AddItem(pagx);
layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals(layoutname));
return layoutItem.GetLayout();

}

Is there another method to add a layout to the project or is it a known bug? I saw other posts with problems on changing map frames on layouts.

I also used the ArcGIS Diagnostic Monitor and the process bugged on 

ArcGIS.Desktop.Internal.Layouts.Ribbon.MapSeriesGalleryItem.RefreshAsync+
100 05:23:41:595 431439 431439 0 0 

I hope somebody can tell me what the problem is.

0 Kudos
3 Replies
PrashantKirpan
Occasional Contributor

Hi,

I've executed your code on ArcGIS Pro 2.7 and it's working fine.

-Prashant 

0 Kudos
ThanHtetAung_EsriAu
Esri Regular Contributor

Hi @MyriamFischer ,

I have also encountered similar issue before.

The problem is at   >> return layoutItem.GetLayout();

When I debug, I noticed layoutItem is null sometime. 

Since it is in async and didn't catch error, you got that error message.

 

It might be because of async thread issue, after item is added, we cannot retrieved straight.

So I have done workaround by project item change event and capture the added item.

After that quickly unsubscribed it.

 

here is the code snippet, I used before -

 this.ProjectItemChangeSubscription = ArcGIS.Desktop.Core.Events.ProjectItemsChangedEvent.Subscribe(this.CurrentProjectItemChangeEvent);

IProjectItem pagx = ItemFactory.Instance.Create("pagx file path") as IProjectItem;
                                ps.Message = "Importing pagx";
                                IEnumerable<IProjectItem> projectItems = await QueuedTask.Run(() =>
                                {
                                    return Project.Current.ImportItem(pagx as IProjectMultiItem, true);
                                }, ps.Progressor);
 ArcGIS.Desktop.Core.Events.ProjectItemsChangedEvent.Unsubscribe(this.ProjectItemChangeSubscription);


  private void CurrentProjectItemChangeEvent(ProjectItemsChangedEventArgs obj)
        {
            if (obj.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add && obj.ProjectItem.GetType() == typeof(LayoutProjectItem) && this.getProjectItem)
            {
                this.AddedProjectItem = obj.ProjectItem as LayoutProjectItem;
            }
        }

 

0 Kudos
MyriamFischer
New Contributor II

Thank you for your suggestion. The problem is, I still have to get the layout from the LayoutProjectItem with GetLayout(); But at least the ImportItem option is faster than to get the item first and then open it. Anyway the layout only hangs when I'm activating the mapframe.

But what I tested now, is to open a layout first with the import layout from the catalog pane and then opening a layout with my code. The interesting thing is, my layout opened by code doesn't hang when I open one with the catalog option first.

0 Kudos