how to do a hittest in mvvm patten

5823
2
Jump to solution
05-19-2015 06:15 AM
JohnWoon
New Contributor

Hi all,

I'm trying to separate MapView and Map. Following the guide "Use the MVVM design pattern—ArcGIS Runtime SDK for .NET | ArcGIS for Developers ".

But it seems unable to a hittest in the viewmodel, because the viewmodel have no reference to the MapView and the hittest method need a MapView  as a parameter.

I implemented a EventToCommand trigger to bind the MapViewTapped event to a RelyCommand, using the mvvm light toolkit library provided by MVVM Light Toolkit  .

I could get the EventArgs (Esri.ArcGISRuntime.Controls.MapViewInputEventArgs), but not the sender which should be the instance of the MapView control.

Hope somebody show me a better solution, thanks in advance.

0 Kudos
1 Solution

Accepted Solutions
AnttiKajanus1
Occasional Contributor III

Easiest way is to do HitTest in the View level (hit test is done against rendered view) and then pass that information (found graphics) to the ViewModel. This won't violate MVVM pattern itself but if you can avoid all code in code behind by using 'View Service' / 'Controller' concept where you abstract MapView usage. We use this approach a lot and you can find similar case when you want to set MapViews viewpoint from ViewModel. Morten wrote a sample for this and you can easily extent this to include hit testing.  [blogpost] Navigating the MapView from a ViewModel You can also check editing demo code from last year. Please note that we haven't updated that repository but the concept still applies.

View solution in original post

0 Kudos
2 Replies
AnttiKajanus1
Occasional Contributor III

Easiest way is to do HitTest in the View level (hit test is done against rendered view) and then pass that information (found graphics) to the ViewModel. This won't violate MVVM pattern itself but if you can avoid all code in code behind by using 'View Service' / 'Controller' concept where you abstract MapView usage. We use this approach a lot and you can find similar case when you want to set MapViews viewpoint from ViewModel. Morten wrote a sample for this and you can easily extent this to include hit testing.  [blogpost] Navigating the MapView from a ViewModel You can also check editing demo code from last year. Please note that we haven't updated that repository but the concept still applies.

0 Kudos
JohnWoon
New Contributor

Thanks for Antti Kajanus.

I decide to hold a weak reference of the MapView in the ViewModel using AttachedProperty tech..

I think it's better to hold the mapview reference (or weak reference) in the ViewModel directly rather than having a MapViewController in between when we need to access the map at the same time, since the map is defined in the ViewModel and is heavy business logic relative.

0 Kudos