I'm working on an AddIn that should produce rasters and pdf from multiple layout projects. For every project defined, it should load it and do some work based on user input and preferences, producing TIFFs and PDFs requested.
The project list is dynamic and defined by the user. With a single project the goal is easily achievable listening to the MapViewInitializedEvent, so after the MapView is initialized I can access to it and run all the code I need, but I need to figure out a way to do it with multiple projects inside a cycle.
The cycle should run through all the projects in the list and do the same thing, with a pseudo-code similar to this:
foreach (var project in ProjectList)
{
string currentProjectPath = Path.Combine(baseFolderPath, project.BasePath, project.FileName);
if (!File.Exists(currentProjectPath))
continue;
Project.Current.SetDirty(false);
if (_mapViewInitEventToken != null)
MapViewInitializedEvent.Unsubscribe(_mapViewInitEventToken);
_mapViewInitEventToken = MapViewInitializedEvent.Subscribe(OnMapViewInitialized);
await Project.OpenAsync(currentProjectPath);
}
The OnMapViewInitialized handler is the entry point from which I am able to run the code I need. Now, this code will never work and I can understand why: after a project is opened, the code goes on to the next project in the list and another project is loaded before MapViewInitialized Event has the chance to be fired. In fact if I run this code I only get the last project in the list correctly worked.
I need some other event or method that allows my code to wait for the work I need to do on a project before going on to the next one. Listening to ProjectOpened event does not help, since I can't get to access the map from there. Anyone with a suggestion would be highly appreciated, thanks