Hi
I have a WPF Prism app with a MapView which is getting a Map from the viewmodel.
MainMapUC.xaml
<esri:MapView
x:Name="MainMapView"
Map="{Binding Map}">
<ei:Interaction.Behaviors>
<bh:MapViewLocationDataChangedBehavior Command="{Binding UpdateLocationCommand}" />
</ei:Interaction.Behaviors>
</esri:MapView>
I created a custom LocationDataSource by extending the `LocationDataSource` abstract class, exposed it via `IGeoLocationService` , and attached this location source to the MapView (later, I also used the `SimulatedLocationDataSource`).
MainMapUC.xaml.cs
public MainMapUC(IGeoLocationService geoLocationService, IEventAggregator eventAggregator)
{
InitializeComponent();
MainMapView.LocationDisplay.DataSource = geoLocationService.GetLocationDataSource();
MainMapView.LocationDisplay.AutoPanMode = Esri.ArcGISRuntime.UI.LocationDisplayAutoPanMode.Recenter;
eventAggregator.GetEvent<GeoLocationEnabledEvent>().Subscribe(enable => MainMapView.LocationDisplay.IsEnabled = enable);
}
I would like to get the Location object when it changes so I thought to hook up to the `LocationDataSource.LocationChanged` event but it's never fired.
I found this solution that is exposing MapView properties via Behavior which is how I am hooking up to the event.
https://github.com/marceloctorres/GeonetPost.WPF/blob/master/GeonetPost.WPF/
public class MapViewLocationDataChangedBehavior : Behavior<MapView>
{
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register(
nameof(Command),
typeof(ICommand),
typeof(MapViewLocationDataChangedBehavior)
);
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
protected override void OnAttached()
{
this.AssociatedObject.LocationDisplay.DataSource.LocationChanged += DataSource_LocationChanged;
}
protected override void OnDetaching()
{
this.AssociatedObject.LocationDisplay.DataSource.LocationChanged -= DataSource_LocationChanged;
}
private void DataSource_LocationChanged(object sender, Esri.ArcGISRuntime.Location.Location e)
{
if (this.Command != null)
{
if (this.Command.CanExecute(e))
{
this.Command.Execute(e);
}
}
}
}
MainMenuVM.cs
internal class MainMapVM : BindableBase
{
private IEventAggregator _eventAggregator;
public MainMapVM(IEventAggregator eventAggregator)
{
Map = new Map()
{
Basemap = new Basemap(BasemapStyle.ArcGISStreets),
InitialViewpoint = new Viewpoint(59, 6, 1000000),
};
UpdateLocationCommand = new DelegateCommand<Location>(UpdateLocationAction, CanUpdateLocation);
_eventAggregator = eventAggregator;
}
private void UpdateLocationAction(Location newLocation)
{
_eventAggregator.GetEvent<GeoLocationChangedEvent>().Publish(newLocation);
}
private bool CanUpdateLocation(Location newLocation)
{
return true;
}
public ICommand UpdateLocationCommand { get; private set; }
private Map _map;
public Map Map
{
get { return _map; }
set { SetProperty(ref _map, value); }
}
}
No matter what I do, I couldn't get the LocationDataSource events to fire.