2013-05-07 27 views
6

Đây là đoạn mã XAML:MVVM Windows Phone 8 - thêm một bộ sưu tập các đinh ghim vào một bản đồ

<maps:Map x:Name="NearbyMap" 
        Center="{Binding MapCenter, Mode=TwoWay}" 
        ZoomLevel="{Binding ZoomLevel, Mode=TwoWay}" 
       > 
     <maptk:MapExtensions.Children> 
      <maptk:MapItemsControl Name="StoresMapItemsControl" ItemsSource="{Binding Treks}"> 
       <maptk:MapItemsControl.ItemTemplate> 
        <DataTemplate> 
         <maptk:Pushpin x:Name="RouteDirectionsPushPin" GeoCoordinate="{Binding Location}" Visibility="Visible" Content="test"/> 
        </DataTemplate> 
       </maptk:MapItemsControl.ItemTemplate> 
      </maptk:MapItemsControl> 
      <maptk:UserLocationMarker x:Name="UserLocationMarker" Visibility="Visible" GeoCoordinate="{Binding MyLocation}"/> 
     </maptk:MapExtensions.Children> 
    </maps:Map> 

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

PushPinModel có Location thuộc tính mà là một GeoCoordinate. TreksObservableCollection<PushPinModel>. Tôi chạy mã này và chỉ hiển thị UserLocationMarker, vị trí hiện tại của tôi.

Trả lời

16

Cuối cùng tôi làm cho nó hoạt động bằng cách sử dụng thuộc tính phụ thuộc. Tôi đã thêm một lớp mới:

public static class MapPushPinDependency 
{ 
    public static readonly DependencyProperty ItemsSourceProperty = 
      DependencyProperty.RegisterAttached(
      "ItemsSource", typeof(IEnumerable), typeof(MapPushPinDependency), 
      new PropertyMetadata(OnPushPinPropertyChanged)); 

    private static void OnPushPinPropertyChanged(DependencyObject d, 
      DependencyPropertyChangedEventArgs e) 
    { 
     UIElement uie = (UIElement)d; 
     var pushpin = MapExtensions.GetChildren((Map)uie).OfType<MapItemsControl>().FirstOrDefault(); 
     pushpin.ItemsSource = (IEnumerable)e.NewValue; 
    } 


    #region Getters and Setters 

    public static IEnumerable GetItemsSource(DependencyObject obj) 
    { 
     return (IEnumerable)obj.GetValue(ItemsSourceProperty); 
    } 

    public static void SetItemsSource(DependencyObject obj, IEnumerable value) 
    { 
     obj.SetValue(ItemsSourceProperty, value); 
    } 

    #endregion 
} 

Và trong file .xaml Tôi đã thêm

xmlns:dp="clr-namespace:Treks.App.Util.DependencyProperties" 

và bây giờ các tập tin .xaml trông như thế này:

<maps:Map x:Name="NearbyMap" 
        Center="{Binding MapCenter, Mode=TwoWay}" 
        ZoomLevel="{Binding ZoomLevel, Mode=TwoWay}" 
        dp:MapPushPinDependency.ItemsSource="{Binding Path=Treks}" 
       > 
     <maptk:MapExtensions.Children> 
      <maptk:MapItemsControl Name="StoresMapItemsControl"> 
       <maptk:MapItemsControl.ItemTemplate> 
        <DataTemplate> 
         <maptk:Pushpin x:Name="PushPins" GeoCoordinate="{Binding Location}" Visibility="Visible" Content="test"/> 
        </DataTemplate> 
       </maptk:MapItemsControl.ItemTemplate> 
      </maptk:MapItemsControl> 
      <maptk:UserLocationMarker x:Name="UserLocationMarker" Visibility="Visible" GeoCoordinate="{Binding MyLocation}"/> 
     </maptk:MapExtensions.Children> 
    </maps:Map> 

Bây giờ tất cả các đinh ghim được hiển thị chính xác.

+0

Điều này giải quyết được vấn đề của tôi, cách tiếp cận tuyệt vời – xximjasonxx

+0

Cảm ơn đã giải quyết sự cố của tôi – robertk

+0

Bạn cũng có thể cung cấp mã cho Page.xaml.cs - Tôi không biết cách tôi có thể ràng buộc danh sách thực tế của các mục bản đồ (tức là 'Bản nhạc ') để' Map' hoặc 'MapItemsControl' –

3

MapItemsControl hiện chưa được MVVM ràng buộc (những gì tôi biết). Cách tốt nhất là đặt ItemsSource vào mã phía sau chế độ xem của bạn.

Bạn vẫn có thể sử dụng bộ sưu tập được xác định trong ViewModel của mình! Tùy chọn là:

  • thông qua tin nhắn MVVM vượt qua cùng bộ sưu tập từ viewmodel vào mã đằng sau những cái nhìn
  • sử dụng các DataContext của chế độ xem để truy cập vào bộ sưu tập, một cái gì đó như thế này: this.StoresMapItemsControl.ItemsSource = ServiceLocator.Current.GetInstance<MainViewModel>().Locations;
Các vấn đề liên quan