In ArcGIS Pro 2.9, OnDrawComplete event doesn't always wait until drawing is complete

916
9
01-06-2022 07:25 AM
BarbaraSchneider2
Occasional Contributor II

In my ArcGIS Pro Add-In, when a project is started, I wait until drawing is complete using the OnDrawComplete event. I then access the map using MapView.Active.Map. But sometimes, the map is null. This only happens since I installed 2.9. In previous versions, this worked fine.

I noticed that the TOC draws after the map drawing is complete. The map is null before the TOC drawing is complete.

0 Kudos
9 Replies
BarbaraSchneider2
Occasional Contributor II

I have found a workaround for this problem. When I make the TOC as small as possible (remove unused layers, collapse layers), the problem doesn't appear.

Tags (2)
0 Kudos
RishabhRaj29
New Contributor II

please share the code snippet for DrawComplete Event

0 Kudos
BarbaraSchneider2
Occasional Contributor II
internal class MyModule : Module
{  
  private static SubscriptionToken EventTokenDrawComplete { get; set; }

  /// <summary>
  /// Initializes module. Is called when ArcGIS pro starts
  /// </summary>
  /// <returns></returns>
  protected override bool Initialize()
  {
    EventTokenDrawComplete = DrawCompleteEvent.Subscribe(OnDrawComplete);
    return base.Initialize();
  }
  
  /// <summary>
  /// Called by Framework when ArcGIS Pro is closing
  /// </summary>
  /// <returns>False to prevent Pro from closing, otherwise True</returns>
  protected override bool CanUnload()
  {
    DrawCompleteEvent.Unsubscribe(OnDrawComplete);
    return true;
  }

  /// <summary>
  /// Is called after drawing is complete. Is used when project is opened.
  /// </summary>
  /// <param name="args"></param>
  private void OnDrawComplete(MapViewEventArgs args)
  {
    Add your code here to initialize your project
    if (EventTokenDrawComplete != null)
    {
      // We unsubscribe to the event here because we only want to 
      // listen to the event when project is opened
      DrawCompleteEvent.Unsubscribe(EventTokenDrawComplete);
      EventTokenDrawComplete = null;
    }       
  }
}
        
0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi,

I would suggest you another workflow for event subscribing. At first in Initialize() subscribe to ProjectOpenedEvent.

        protected override bool Initialize()
        {

                ProjectOpenedEvent.Subscribe(OnProjectOpen);
        }

Then in OnProjectOpen method subscribe to DrawCompleteEvent

        private SubscriptionToken _drawCompleteEventToken;

        private void OnProjectOpen(ProjectEventArgs args)
        {
            _drawCompleteEventToken = DrawCompleteEvent.Subscribe(OnDrawComplete);
        }
0 Kudos
RishabhRaj29
New Contributor II

Thank you for sharing the snippet @BarbaraSchneider2 .

When I am clicking the Open Project tool to open the project; below code in Module1.cs runs:

protected override bool Initialize()
  {
    EventTokenDrawComplete = DrawCompleteEvent.Subscribe(OnDrawComplete);
    return base.Initialize();
  }

and then the pointer goes to my OpenMap.cs file where Project.OpenAsync(projectPath); runs and opens the project.

However, just after that the next lines of code start executing where I am working with Active Map View which is still null in this case.

But, when I put a MessageBox just after opening the project, below onDrawComplete event handler is called:

private void OnDrawComplete(MapViewEventArgs args)
  {
    Add your code here to initialize your project
    if (EventTokenDrawComplete != null)
    {
      // We unsubscribe to the event here because we only want to 
      // listen to the event when project is opened
      DrawCompleteEvent.Unsubscribe(EventTokenDrawComplete);
      EventTokenDrawComplete = null;
    } 

after which the map view is not null. 

Any suggestions?

0 Kudos
BarbaraSchneider2
Occasional Contributor II
RishabhRaj29,
I don't really understand what you are doing. Are you opening the project manually or programmatically?
0 Kudos
RishabhRaj29
New Contributor II

Programatically. 

I have a tool which when on clicked, opens a project from the path I have set. After opening the project, I am adding layers to the map programmatically. But the issue I am facing is that view is returning null since it’s not loaded. 

 

0 Kudos
BarbaraSchneider2
Occasional Contributor II
RishabhRaj29,
the map is not returning null any more when you access it in the OnDrawComplete() method. Do I understand you right that the OnDrawComplete() method is not called unless you put a message box just after opening the project?
 
As a workaround you could already have included a (basemap) layer in the project that you are opening.
0 Kudos
RishabhRaj29
New Contributor II

@BarbaraSchneider2 ,

When I put a message box right after await Project.OpenAsync(ProjectPath); OnDrawComplete event is called that time, but still when I access map view, it’s returning null. 

 

When I remove the message box, the OnDrawComplete event is called after the project opens and some more lines of code to log the details are run, then also, map view is returning null. 

 

I am not sure that how OnDrawComplete which is an event that only gets called after drawing completes automatically, is returning map view as null. 

 

I will try to add a basemap to the map and then try. Will keep you posted. 

0 Kudos