C# 从mvvm中的bing地图点击事件获取位置
当我点击bing地图控件时,我正在尝试获取位置。我知道如何获得它,但我想在ViewModel中实现它。对于Tap事件,我使用交互C# 从mvvm中的bing地图点击事件获取位置,c#,windows-phone-7,mvvm,bing-maps,C#,Windows Phone 7,Mvvm,Bing Maps,当我点击bing地图控件时,我正在尝试获取位置。我知道如何获得它,但我想在ViewModel中实现它。对于Tap事件,我使用交互 <map:Map CopyrightVisibility="Collapsed" LogoVisibility="Collapsed"> <map:MapTileLayer> <map:MapTileLayer.TileSources>
<map:Map CopyrightVisibility="Collapsed" LogoVisibility="Collapsed">
<map:MapTileLayer>
<map:MapTileLayer.TileSources>
<customMap:OpenStreetTile />
</map:MapTileLayer.TileSources>
</map:MapTileLayer>
<map:MapLayer>
<map:Pushpin Location="{Binding Location}" />
</map:MapLayer>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand Command="{Binding AddPushpinCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</map:Map>
但我不确定这是否是一个好的解决方案。你知道怎么做吗?
谢谢好吧,你的方法肯定管用,有时候,足够好就足够好了但是,如果希望MVVM实现100%干净,则应避免引用视图(因此也应避免引用MapLayer类)
Joost van Schaik写了一篇博客文章,展示了如何使用行为实现这一点(适用于Windows Phone 8,但我相信它也适用于Windows Phone 7):谢谢这是我所需要的。
var source = (e.OriginalSource as MapLayer).ParentMap;
var point = e.GetPosition(source);
Location = source.ViewportPointToLocation(point);