3

当我点击 bing 地图控件时,我正在尝试获取位置。我知道如何获得它,但我想在 ViewModel 中制作它。对于 Tap 事件,我使用交互。

<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>

我可以使用 GetPosition 函数从事件参数中获取位置。但是这个函数需要 UIElement 参数。我是这样做的:

var source = (e.OriginalSource as MapLayer).ParentMap;
        var point = e.GetPosition(source);
        Location = source.ViewportPointToLocation(point);

但我不确定这是否是一个好的解决方案。你知道怎么做吗?谢谢

4

1 回答 1

2

好吧,您的方法确实有效,有时,足够好就足够了。;) 如果你想 100% 干净地使用你的 MVVM 实现,你应该避免引用视图(以及 MapLayer 类)。

Joost van Schaik 写了一篇博客文章,展示了如何使用行为来实现这一点(它适用于 Windows Phone 8,但我确信它也适用于 Windows Phone 7):http ://dotnetbyexample.blogspot.nl/2013/03 /simple-reverse-geocoding-with-windows.html

于 2013-04-01T12:25:34.440 回答