MapOverlay 绑定不起作用 [英] MapOverlay binding not working

查看:20
本文介绍了MapOverlay 绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 MVVM 代码结构在 Windows Phone 8 上获取地图叠加层.我似乎无法让 MapOverlay 的 GeoCoordinate 属性正确绑定到我的 ViewModel,我不知道为什么.

I'm trying to get a map overlay working on Windows Phone 8 using an MVVM code structure. I can't seem to get the GeoCoordinate property of the MapOverlay to bind to my ViewModel properly and I can't work out why.

所代表的 XAML 是:

The XAML as stands is:

标题:

xmlns:maps="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps"

代码:

<maps:Map x:Name="Map"  ZoomLevel="6" Height="500" Width="500" CartographicMode="Terrain" Center="{Binding MapCenter, Converter={StaticResource GpsCoordinateConverter}, Mode=TwoWay}">
    <maps:Map.Layers>
        <maps:MapLayer>
            <maps:MapOverlay PositionOrigin="1,1"  GeoCoordinate="{Binding MapCenter, Converter={StaticResource GpsCoordinateConverter}, Mode=OneWay}">
                <Ellipse Fill="Blue" Height="20" Width="20" Opacity="50" />
            </maps:MapOverlay>
        </maps:MapLayer>
    </maps:Map.Layers>
</maps:Map>

GpsCoordinateConverter 只是一个非常简单的类,它将数据类型从我的视图模型更改为地图控件所期望的 System.Device.Location.GeoCoordinate.地图绑定的中心工作正常,但叠加层上的地理坐标不会绑定,蓝色圆圈只是位于地图的左上角.

The GpsCoordinateConverter is just a very simple class which changes the data type from my View Model into the System.Device.Location.GeoCoordinate that the map control expects. The center of the map binding is working fine, but the GeoCoordinate on the overlay will not bind and the blue circle just sits in the top left of the map.

我已经验证了 PropertyChanged 事件正在为我的模型触发,无论是通过调试还是因为地图本身的中心正在更新,甚至尝试传递 null 以触发所有字段都无济于事.

I've verified the PropertyChanged event is firing for my model, both through debug and because the center of the map itself is updating, and even tried passing null to trigger all fields to no avail.

我已经检查了调试并且 MapOverlay GeoCoordinate 的属性似乎总是​​为空.我尝试将以下代码添加到代码隐藏中,将圆圈放在我想要的位置,但我似乎无法根据事件让它发生......

I've checked in debug and the property for the MapOverlay GeoCoordinate always seems to be null. I've tried adding the following to the code-behind which puts the circle where I want it, but I can't seem to get it to happen based on the event...

GpsCoordinateConverter converter = new GpsCoordinateConverter();
Map.Layers[0][0].GeoCoordinate = (GeoCoordinate)converter.Convert(((ViewModel.ReportViewModel)DataContext).MapCenter, typeof(GeoCoordinate), null, null);

有谁知道为什么会这样或如何解决这个问题?我不想为此放弃 MVVM 架构.

Does anyone know why this would be or how to fix this? I'd prefer not to abandon the MVVM architecture for this.

推荐答案

这是 Silverlight XAML 解析器的一个限制.初始化为 XAML 元素属性的一部分的对象不会参与同一逻辑树,因此没有数据上下文深入到它们中.

That's a limitation of the Silverlight XAML Parser. Objects initialized as part of XAML elements' properties will not participate in the same logical tree and as such don't have datacontext drilling down into them.

为了对新的诺基亚地图控件进行数据绑定,请使用新的 Windows Phone Toolkit 中的 MapExtensions.例如,这里是如何使用 MapExtensions 在特定地理坐标中创建 PushPin.

In order to databind the new Nokia Map control, use MapExtensions from the new Windows Phone Toolkit. For example here's how to create a PushPin in a specific GeoCoordinate using MapExtensions.

<maps:Map x:Name="Map" Grid.Row="1" Hold="OnMapHold">
    <maptk:MapExtensions.Children>
        <maptk:Pushpin x:Name="RouteDirectionsPushPin" Visibility="Collapsed"/>
        <maptk:MapItemsControl Name="StoresMapItemsControl">
            <maptk:MapItemsControl.ItemTemplate>
                <DataTemplate>
                    <maptk:Pushpin GeoCoordinate="{Binding GeoCoordinate}" Visibility="{Binding Visibility}" Content="{Binding Address}"/>
                </DataTemplate>
            </maptk:MapItemsControl.ItemTemplate>
        </maptk:MapItemsControl>
        <maptk:UserLocationMarker x:Name="UserLocationMarker" Visibility="Collapsed"/>
    </maptk:MapExtensions.Children>
</maps:Map>

这篇关于MapOverlay 绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆