MVVM Windows Phone 8 - 向 map 添加图钉集合

标签 mvvm windows-phone-8 mvvm-light bing-maps pushpin

这是 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"

图钉型号 有一个属性 Location,它是一个 GeoCoordinate。 徒步旅行 ObservableCollection<PushPinModel> .我运行此代码,只有 用户位置标记 显示,这是我当前的位置。

最佳答案

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

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>

现在所有图钉都正确呈现。

关于MVVM Windows Phone 8 - 向 map 添加图钉集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16417978/

相关文章:

c# - WPF OpenFileDialog 与 MVVM 模式?

silverlight - 使用 MVVM Light 从 ViewModel 查看通知

silverlight - 使用MVVM Light将消息发送到Silverlight中的未构造类

android - 可以使用每次返回新 LiveData 的存储库功能从 UI 刷新 LiveData?

c# - TabItem/TabControl 中的 DataGridTextColumn 标题绑定(bind)问题

c# - DeviceNetworkInformation.IsCellularDataEnabled 始终返回 false

c# - Windows Phone 8.1 Silverlight 和 XAML

ruby-on-rails - iframe 导致无法验证 Rails 中的 CSRF token 真实性

silverlight - 对象列表框和按钮

c# - 如何最好地应用WPF MVVM?