MVVM Windows Phone 8 – 向地图添加图钉集合

这是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,这是我当前的位置.

我终于使用依赖属性使其工作.我添加一个新类:
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}"
                  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>

现在所有的图钉都被正确渲染.

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...