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.
please share the code snippet for DrawComplete Event
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;
}
}
}
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);
}
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?
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.
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.