c# - 使用 mvvm 模型在 Windows Phone 中的 map 上显示多个图钉

标签 c# windows-phone-7 mvvm windows-phone-8 windows-phone

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

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

在每隔几米触发的地理定位器positionchanged事件中,我设置绑定(bind)属性“PushPinLocation”的值(来 self 的 View 模型),这对于图钉和位置标记来说很常见。

//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");
        }
    }
}

在同一个 View 模型 geolocator_Position 更改事件中,我设置图钉位置:

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

但我总是看到最新的出现,而旧的从未显示在 map 上。有什么办法可以保留旧的。

最佳答案

这篇文章已有一年了,但尚未得到答复,所以这是我的答案:

不要绑定(bind)到单个 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());
}

这会将新位置添加到列表中,只要它绑定(bind)到此位置列表,所有图钉都会显示。

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

关于c# - 使用 mvvm 模型在 Windows Phone 中的 map 上显示多个图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19831212/

相关文章:

C# Linq,在两个列表中搜索相同的项目

windows-phone-7 - Windows Phone 7 : post some values, 检索响应

c# - 为 ItemsControl 的每一项创建一个 ViewModel

c# - 如何将数据从 View 传递到 View 模型

c# - 如何使用诺基亚 API 获取手机驱动器的大小(免费,总计)?

c# - 将 for 循环转换为并行循环

windows-phone-7 - 如何解释来自麦克风的音频流以检测 WP7 中的特定声音?

wcf - 添加用于 WCF 服务和 Windows Phone 7 应用程序的枚举类型

silverlight - 在 Silverlight 4 中使用带有 MVVM 模式的行为和动画

c# - 在以另一个开头的字符串中查找所有命名组的出现