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();
}
}
}
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:
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
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
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:
Thanks
Uma
Hi @UmaHarano , the link is broken. Can you please share the sample again? Thanks!
Hi Uma,
this works fine for MapView, but is there any way to do something similar for LayoutView? I tried different events, but none of them works for me.
Radek
Hi,
Try to subscribe to ArcGIS.Desktop.Layouts.Events.LayoutViewEvent and check args.Hint == LayoutViewEventHint.DrawingComplete. LayoutViewEventHint Enumeration here.
Hi Uma,
this works, thank you!