I am unable to add a layer to the mapview from a button click using the .net MVVM pattern in WPF. I have rigged the button using the ICommand interface and can activate the function in my MapViewModel but I have no clue how to add a featurelayer because I can't use my x:Name="mapView". What I want is to click my button and activate the AddAnotherLayer method, which I can do but I have no idea how to access the mapview to do this.
Does anyone have a simple example on how to do this without using galasoft or prism, just regular MVVM.
help....
MapViewModel.cs
public class MapViewModel : INotifyPropertyChanged
{
private Map _map = new Map(Basemap.CreateImageryWithLabels());
public RadarCommand RadarCommand { get; set; }
public Map Map
{
get { return _map; }
set { _map = value; OnPropertyChanged(); }
}
public MapViewModel()
{
this.RadarCommand = new RadarCommand(this);
CreateNewMap();
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var propertyChangedHandler = PropertyChanged;
if (propertyChangedHandler != null)
propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
}
public async void CreateNewMap()
{
FeatureLayer trailHeadsLayer = new FeatureLayer(new Uri("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0"));
await trailHeadsLayer.LoadAsync();
Map.OperationalLayers.Add(trailHeadsLayer);
Console.WriteLine("all done loading");
}
public async void AddAnotherLayer() {
FeatureLayer featureLayer = new FeatureLayer(new Uri("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0"));
await featureLayer.LoadAsync();
Map.OperationalLayers.Add(featureLayer);
Console.WriteLine("all trail lines done loading");
}
public event PropertyChangedEventHandler PropertyChanged;
}
MainWindow.xaml
<Window.Resources>
<local:MapViewModel x:Key="MapViewModel" />
</Window.Resources>
<Grid>
<esri:MapView Map="{Binding Map, Source={StaticResource MapViewModel}}" x:Name="mapView" />
<uc:TopToolbar/>
</Grid>