How to wait for MapView to Load?

4235
5
10-24-2017 01:05 PM
MKa
by
Occasional Contributor III

In my configuration project, my startup page allows a user to pick a "Location", using this location I create a new project for them.  I then want to get the MapView and load up some feature layers for that location and zoom to that location.  But it seems i have to wait for the Map to load before i can do what I want.  

So I call this Command from my xaml BlankProjectCommand

public ICommand BlankProjectCommand
{
      get
      {
         return new RelayCommand((args) => BlankSelectionViewModelHelper.NewBlankProject(_Location), () => true); ;
      }
}

Then it returns this function NewBlankProject, but the following function wont work fully because i cant reference the MapView.Active.Map as I want to load some feature layers for the Location and zoom to that location.  Is there anyway I can wait for the map to load after var newProject = await Project.CreateAsync(cps);

internal static async void NewBlankProject(string inLocation)
{
     string defaultName = inLocation;
     string defaultFolder = DefaultFolder();
     string templatePath = GetDefaultMapTemplate(); //Map

     if (!string.IsNullOrEmpty(defaultFolder) && !string.IsNullOrEmpty(templatePath))
     {
          var cps = new CreateProjectSettings()
          {     

               Name = defaultName,
               LocationPath = DefaultFolder(),
               TemplatePath = GetDefaultMapTemplate()
          };

          var newProject = await Project.CreateAsync(cps);

          if (MapView.Active != null && MapView.Active.Map != null)
          {
               MapSettings newMapSettings = new MapSettings();
               newMapSettings.PlantCode = inPlantCode;
               newMapSettings.HarvestYear = inHarvestYear;
               newMapSettings.HarvestTrimester = inHarvestTrimester;
               newMapSettings.MaterialGroup = inMaterialGroup;
               newMapSettings.Environment = Settings.CurrentEnvironment;

               List<UserPlantData> UserPlantDataList = new List<UserPlantData>();
               UserPlantDataList = await WebServiceUtility.GetUserPlantDataAsync();


               if (!newMapSettings.Equals(Settings.CurrentMapSettings))
               {
                    await FeatureServiceManagement.LoadFeatureServiceAsync(newMapSettings);
                    await Navigation.ZoomToPlantExtentAsync();
               }

     }
}

Tags (1)
0 Kudos
5 Replies
UmaHarano
Esri Regular Contributor

Hi

When you open a project with a Map View, you can wait for the MapView to initialize by subscribing to the "MapViewInitialized" event (topic12855.html).  Important thing to note is that when you open a new view in your project, this event will be triggered again.

If you want to subscribe to the MapViewInitialized event as a one shot per project, you will have to:

  • Unsubscribe after the MapView has initialized the first time when the project opens.
  • Then subscribe to the ProjectOpenedEvent and then check if the MapView is again initialized. (for subsequent Project open routines)

Here is a code snippet to subscribe and unsubscribe to the MapViewInitialized event.

private SubscriptionToken _eventToken = null;
 protected override bool Initialize()  //Module initialized
        {

            _eventToken = MapViewInitializedEvent.Subscribe(OnMapViewInitialized);
            return base.Initialize();
        }

        private void OnMapViewInitialized(MapViewEventArgs args)
        {

            MessageBox.Show("MapView has been initialized");
            if (_eventToken != null)
            {
                ArcGIS.Desktop.Mapping.Events.MapViewInitializedEvent.Unsubscribe(_eventToken);
                _eventToken = null;
            }
        }

Thanks

Uma

BarbaraSchneider2
Occasional Contributor II

Hi Uma,

thanks for your input. Your code above works fine for me when I first show the message box ("MapView has been initialized") and then access the active map (MapView.Active.Map). When I remove the message box, however, the active mapview is null. How can I get around this?

Thanks,

Barbara

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Barbara

The best way to ensure that the MapView initialization is complete and a MapView is available is to use the DrawCompleteEvent.  

The arcgis-pro-sdk-community-samples repo has a few examples:

https://github.com/Esri/arcgis-pro-sdk-community-samples/search?q=DrawCompleteEvent&unscoped_q=DrawC...

Thanks

Uma

RishabhRaj29
New Contributor II

Hi @UmaHarano , the link is broken. Can you please share the sample again? Thanks!

0 Kudos
BarbaraSchneider2
Occasional Contributor II

Hi Uma,

this works, thank you!

0 Kudos