【问题标题】:MVVM Windows Phone 8 - adding a collection of pushpins to a mapMVVM Windows Phone 8 - 向地图添加图钉集合
【发布时间】:2013-05-07 11:34:37
【问题描述】:

这里是 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。 TreksObservableCollection&lt;PushPinModel&gt;。我运行此代码,只显示 UserLocationMarker,这是我当前的位置。

【问题讨论】:

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


    【解决方案1】:

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

    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 或 @987654326 @
    • 解决了我的问题,我无法使用名称访问 MapItemsControl:ObservableCollection&lt;DependencyObject&gt; children = MapExtensions.GetChildren(NearbyMap); MapItemsControl mapItems = children.FirstOrDefault(x =&gt; x.GetType() == typeof(MapItemsControl)) as MapItemsControl; mapItems.ItemsSource = Treks;
    【解决方案2】:

    MapItemsControl 目前还不是 MVVM 可绑定的(据我所知)。 所以最好的方法是在你的视图后面的代码中设置它的ItemsSource

    您仍然可以使用您的 ViewModel 中定义的集合! 选项是:

    • 通过 mvvm 消息传递将集合从视图模型传递到视图背后的代码
    • 使用视图的数据上下文访问集合,如下所示:this.StoresMapItemsControl.ItemsSource = ServiceLocator.Current.GetInstance&lt;MainViewModel&gt;().Locations;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      相关资源
      最近更新 更多