我在基于mvvm模型的应用程序中具有以下视图,该视图应使用视图模型中的绑定属性“ PushPinLocation”显示我绑定到它的所有图钉。

<MapNS:Map
    Center="{Binding MapCenter, Mode=TwoWay}" Margin="0,0,-5,0"
    CartographicMode="{Binding MapMode, Mode=TwoWay}"
    LandmarksEnabled="True" PedestrianFeaturesEnabled="True"
    ZoomLevel="{Binding MapZoomLevel, Mode=TwoWay}"
    Foreground="AliceBlue" Grid.Row="1" Height="713"  Width="425"
    x:Name="mapPanoramaAddress"  >

    <!--Adding Location to show map initially until the data arrived-->
    <maptk:MapExtensions.Children>

        <maptk:MapItemsControl Name="StoresMapItemsControl" >
            <maptk:MapItemsControl.ItemTemplate>
                <DataTemplate>
                     <maptk:Pushpin x:Name="PushPins" Background="White"
                        GeoCoordinate="{Binding PushPinLocation}"
                        Content="{Binding PushPinDisplayText}"
                        Visibility="Visible" />
                </DataTemplate>
            </maptk:MapItemsControl.ItemTemplate>
        </maptk:MapItemsControl>
        <maptk:UserLocationMarker GeoCoordinate="{Binding PushPinLocation}" x:Name="UserLocationMarker" Visibility="Visible" />

    </maptk:MapExtensions.Children>

</MapNS:Map>


在每隔几米触发一次geolocator positionchanged事件中,我设置了图钉和位置标记常用的绑定属性“ PushPinLocation”的值(来自我的视图模型)。

//PushPinLocation
private GeoCoordinate _PushPinLocation = new GeoCoordinate(40.712923, -74.013292);    //cannot assign null
public GeoCoordinate PushPinLocation
{
    get { return _PushPinLocation; }
    set
    {
        if (_PushPinLocation != value)
        {
            _PushPinLocation = value;
            RaisePropertyChanged("PushPinLocation");
        }
    }
}


在同一viewmodel geolocator_Position更改的事件中,我正在设置pushpinlocation:

private void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
    this.PushPinLocation = args.Position.Coordinate.ToGeoCoordinate();
}


但是我总能看到最新的旧地图,而旧的地图却从未显示过,有什么办法我也可以保留旧的地图。

最佳答案

这篇文章已有1年历史了,但未得到解答,所以这是我的答案:

与其绑定到单个PushPinLocation,不如使用集合。在您的ViewModel中,添加以下内容:

private List<GeoCoordinate> _pushPinLocations;
public List<GeoCoordinate> PushPinLocations
{
    get { return _pushPinLocations; }
    set
    {
        _pushPinLocations = value;
        OnPropertyChanged("PushPinLocations");
    }
}


并将您的活动更改为:

private void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
    this.PushPinLocations.Add(args.Position.Coordinate.ToGeoCoordinate());
}


这会将新位置添加到列表中,只要将其绑定到该位置列表,所有图钉都会显示出来。

<maptk:MapItemsControl Name="StoresMapItemsControl" >
    <maptk:MapItemsControl.ItemTemplate>
        <DataTemplate>
             <maptk:Pushpin x:Name="PushPins" Background="White"
                GeoCoordinate="{Binding PushPinLocations}"
                Content="{Binding PushPinDisplayText}"
                Visibility="Visible" />
        </DataTemplate>
    </maptk:MapItemsControl.ItemTemplate>
</maptk:MapItemsControl>

07-24 15:23