MapOverlay绑定无法正常工作 [英] MapOverlay binding not working

查看:107
本文介绍了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是:

标题:

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.地图绑定的中心工作正常,但是叠加层上的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工具包中的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天全站免登陆