Windows Phone 8.1 中 MapControl Center 属性 的数据绑定

Databinding of MapControl Center property in Windows Phone 8.1

我正在使用 MVVM Light 开发一个 windows phone 8.1 (winRT) 应用程序和一个使用 MapControl 的 PCL。 我在绑定 MapControl Center 属性 时遇到问题。 在应用程序初始化时,属性 在 ViewModel 中设置并且地图正确居中。

但是,当我更新 ViewModel 中的值时,地图没有重新居中,但如果我将该值绑定到文本块,它就会正确更新。

XAML :

<Maps:MapControl BorderThickness="2" BorderBrush="Black"
        x:Name="Map" 
        HorizontalAlignment="Right" Margin="0,45,0,0" 
        VerticalAlignment="Top" 
        Height="595"  Width="400"
        ZoomLevel="10"
        LandmarksVisible = "False"
        TrafficFlowVisible = "False"
        PedestrianFeaturesVisible = "False"
        Center="{Binding Path=ViewStoreModel.CenterPosition, Mode=OneWay, Converter={StaticResource NormalizedAnchorPointConverter}}"
        MapServiceToken="{StaticResource MapServiceTokenString}">

            <Maps:MapItemsControl x:Name="MapIcons" ItemsSource="{Binding ViewStoreModel.ListStoreSearch}"  >
                <Maps:MapItemsControl.ItemTemplate>
                    <DataTemplate x:Name="Temp" >
                        <StackPanel Tapped="Image_Tapped" x:Name="MyStack"  Maps:MapControl.Location="{Binding store_position, Converter={StaticResource GeoPointConvertCenter}}">
                            <Image x:Name="PinsImage" Source="ms-appx:///Assets/map-pin-button.png" />
                        </StackPanel>
                    </DataTemplate>
                </Maps:MapItemsControl.ItemTemplate>
            </Maps:MapItemsControl>
        </Maps:MapControl>

属性 的视图模型:

    public Location CenterPosition
        {
            get
            { 
                return _centerPosition;
            }
             set
            {
              Set(CenterPositionPropertyName, ref _centerPosition, value);
            }
        }

public class Location : ObservableObject
{
    public const string latitudePropertyName = "latitude";
    public const string longitudePropertyName = "longitude";
    private double _latitude;
    private double _longitude;
    public double latitude
    {
        get
        {
            return _latitude;
        }
        set
        {
            Set(latitudePropertyName, ref _latitude, value);
        }

    }
    public double longitude
    {
        get
        {
            return _longitude;
        }
        set
        {
            Set(longitudePropertyName, ref _longitude, value);
        }
    }

属性 中心是一种地理点类型,因此我使用转换器将其从自定义 class 位置转换而来。 Center 是一个依赖项 属性,所以它应该是可绑定的。

感谢您的帮助。

您是否尝试删除 "Path" 属性 并将其直接绑定到您的 ViewModel?

<Maps:MapControl Center="{Binding ViewStoreModel.CenterPosition, Mode=OneWay, Converter={StaticResource NormalizedAnchorPointConverter}}"/>

我发现 MapControl 中存在一些错误。 问题来自绑定模式。 OneWay 似乎像 OneTime 一样工作(仅在初始化时)。 如果我使用 TwoWay 它可以工作,但地图会不断更新 ViewModel。

作为解决方法,我指定我们必须明确告知 XAML 何时必须更新源。

代码:

<Maps:MapControl BorderThickness="2" BorderBrush="Black"
        x:Name="Map" 
        HorizontalAlignment="Right" Margin="0,45,0,0" 
        VerticalAlignment="Top" 
        Height="595"  Width="400"
        ZoomLevel="10"
        LandmarksVisible = "False"
        TrafficFlowVisible = "False"
        PedestrianFeaturesVisible = "False"
        Center="{Binding ViewStoreModel.CenterPosition, Mode=TwoWay, UpdateSourceTrigger=Explicit, Converter={StaticResource NormalizedAnchorPointConverter}}"
        MapServiceToken="{StaticResource MapServiceTokenString}"
        >

我们使用了一些不同的方法。
我们使用了 DataContext 的 PropertyChanged 事件——在处理程序中我们检查 e.PropertyName 是否等于 CurrentLocation,如果是,我将调用 NearbySitesMap.SetView(currentLocation, ZoomLevel, NearbySitesMap.Heading);.
这样,当 CurrentLocation 发生变化时,地图会以漂亮流畅的动画显示到新位置。