MVVM Windows Phone 8 - 向地图添加一个推针集合

6

以下是XAML代码:

<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 有一个 Location 属性,它是一个 GeoCoordinate。 Treks 是一个 ObservableCollection<PushPinModel>。 我运行这段代码,只有UserLocationMarker被显示,它是我的当前位置。

2个回答

16

我最终使用依赖属性使它工作。我添加了一个新类:

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
}

在 .xaml 文件中,我已经添加了

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

现在,.xaml 文件看起来像这样:

<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>

现在所有的图钉都被正确地呈现出来。


你能否提供Page.xaml.cs的代码?我不知道如何将实际的地图项列表(即“Tracks”)绑定到“Map”或“MapItemsControl”。 - Tobias Helbich
解决了我的问题,我无法通过名称访问MapItemsControl:ObservableCollection<DependencyObject> children = MapExtensions.GetChildren(NearbyMap); MapItemsControl mapItems = children.FirstOrDefault(x => x.GetType() == typeof(MapItemsControl)) as MapItemsControl; mapItems.ItemsSource = Treks; - Tobias Helbich

3

MapItemsControl 目前还不支持 MVVM 绑定(据我所知)。

因此,最好的方法是在视图的代码后台中设置其 ItemsSource

但您仍然可以使用 ViewModel 中定义的集合!可选方案如下:

  • 通过 MVVM 消息传递将集合从 ViewModel 传递到视图的代码后台
  • 使用视图的数据上下文访问集合,类似于这样:this.StoresMapItemsControl.ItemsSource = ServiceLocator.Current.GetInstance<MainViewModel>().Locations;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接