c# - 在 mvvm 中的 Tap 事件中从 bing map 获取位置

标签 c# windows-phone-7 mvvm bing-maps

当我点击必应 map 控件时,我正在尝试获取位置。我知道如何获取它,但我想在 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 函数从 eventarguments 中获取位置。但是这个函数需要 UIElement 参数。我是这样做的:

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

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

最佳答案

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

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

关于c# - 在 mvvm 中的 Tap 事件中从 bing map 获取位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15743398/

相关文章:

c# - 使用泛型将对象类型转换为泛型类型

c# - 更改 NGUI UILabel 垂直行间距

android - Android、Windows Phone 7 和 Blackberry 的等效配置文件 (iOS)

c# - 解决MVVM项目以及MainViewModel/Model尺寸不断增加的想法?

c# - 从 C# 应用程序执行 C++ 函数

c# - 来自数据库的通知而不是轮询。是否可以?

c# - 如何将文本包装在 TextBlock 中?

c# - 如何检查 wp7 XNA 触摸事件是否在定义的矩形内

Silverlight:业务应用程序需要访问文件才能打印和移动

MVVM在所有 View 之间共享对象