How to use LocationToScreen in MVVM

305
6
Jump to solution
01-09-2018 11:50 AM
MichaelThompson
New Contributor III

I need to covert MapPoints to screen Points in my WPF application. As far as I can tell, this can only be accomplished through the MapView.LocationToScreen() method. What's the best way to access the MapView component from the rest of my application without compromising MVVM design?

Thanks!

Tags (3)
1 Solution

Accepted Solutions
dotMorten_esri
Esri Frequent Contributor

Hmm I don't have good idea off the bat for that. Your offsets would also get invalidated the moment you zoom . It's probably better to have a list of features in you view model, and a converter in your view that converts them to graphics in an overlay.

Also note that you can use the offset properties on the symbol to "move" the points relative to the mappoint, without having to convert the screen coordinates back again.

Another approach is to push the LocationToScreen delegate into your viewmodel on load, and clear out on unload. It's probably not 100% purist MVVM, but neither is dealing with screen coordinates. IMHO a purist MVVM pattern is nice to have to brag about, but it quickly gets somewhat unpractical.

For instance most MVVM experts are clear on stating that it's totally OK to have view-specific code in your code-behind of the view (it's still part of the view), and that might include pushing things to/from the VM, as long what's sent across to the VM isn't view specific stuff in itself. Some purists like to point out how wrong that is, but that's what a purist is supposed to do

View solution in original post

0 Kudos
6 Replies
dotMorten_esri
Esri Frequent Contributor

The simple answer is no you can't without breaking with the MVVM design pattern. The moment you add screen coordinates to your view model, you just brought view-specific stuff into you view model.

Perhaps if you can elaborate a little more why you need this, I can suggest a better way to do this?

0 Kudos
MichaelThompson
New Contributor III

Thanks for assisting, Morten!

I am adding graphics to a GraphicsOverlay using Picture Markers. But, I want to be able to offset items that would collide on the screen. I was hoping to convert the MapPoint to Point using LocationToScreen, apply an x,y offset in screen pixels in cases where graphics would obscure each other, then covert back to a MapPoint using ScreenToLocation.

0 Kudos
dotMorten_esri
Esri Frequent Contributor

Hmm I don't have good idea off the bat for that. Your offsets would also get invalidated the moment you zoom . It's probably better to have a list of features in you view model, and a converter in your view that converts them to graphics in an overlay.

Also note that you can use the offset properties on the symbol to "move" the points relative to the mappoint, without having to convert the screen coordinates back again.

Another approach is to push the LocationToScreen delegate into your viewmodel on load, and clear out on unload. It's probably not 100% purist MVVM, but neither is dealing with screen coordinates. IMHO a purist MVVM pattern is nice to have to brag about, but it quickly gets somewhat unpractical.

For instance most MVVM experts are clear on stating that it's totally OK to have view-specific code in your code-behind of the view (it's still part of the view), and that might include pushing things to/from the VM, as long what's sent across to the VM isn't view specific stuff in itself. Some purists like to point out how wrong that is, but that's what a purist is supposed to do

View solution in original post

0 Kudos
MichaelThompson
New Contributor III

I didn't think about the Symbol offsets. I'll see if that meets my needs before angering the MVVM purity god. Haha!

Thanks a bunch!!

0 Kudos
MichaelThompson
New Contributor III

What is the unit used by OffsetX, OffsetY? The unit of the map's Spatial Reference?

0 Kudos
dotMorten_esri
Esri Frequent Contributor

The units are in device-independent pixels (ie the same as the units you use for your UI layout)

0 Kudos