2015-04-03 114 views
0

我正在開發一個帶有MVVM Light和使用MapControl的PCL的windows phone 8.1(winRT)應用程序。 我遇到了MapControl Center屬性綁定的問題。 在應用程序初始化時,該屬性在ViewModel中設置並且地圖正確居中。Windows Phone 8.1中MapControl Center屬性的數據綁定

但是,當我更新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); 
     } 
    } 

酒店中心是一個類型的GeoPoint所以我用一個轉換器,將其從自定義類的位置轉換。 Center是一個依賴屬性,所以它應該是可綁定的。

感謝您的幫助。

回答

2

我想通了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}" 
     > 
0

你是否嘗試刪除「路徑」屬性並將其直接綁定到您的ViewModel?

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

我試過這個,但沒有奏效。 – Gouar 2015-04-08 07:55:54

1

我們使用有點不同的辦法。
我們使用了DataContext的PropertyChanged事件 - 在處理程序中,我們正在檢查e.PropertyName是否等於CurrentLocation,如果是,我打電話給NearbySitesMap.SetView(currentLocation, ZoomLevel, NearbySitesMap.Heading);
這樣,當CurrentLocation更改時,地圖會獲得一個很好且平滑的動畫到新位置。

+0

工作得很好..而不是propertychanged,你也可以做一個附加屬性,並使用該綁定CurrentLocation ... – 2016-02-19 04:04:32