【发布时间】:2013-11-07 08:37:48
【问题描述】:
我在基于 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");
}
}
}
在同一视图模型 geolocator_Position 更改事件中,我正在设置图钉位置:
private void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
this.PushPinLocation = args.Position.Coordinate.ToGeoCoordinate();
}
但我总是看到最新的显示在地图上,而旧的从不在地图上显示。有什么办法可以保留旧的。
【问题讨论】:
-
您是绑定到特定点还是绑定到点集合?
-
到具有绑定属性“PushPinLocation”的特定点。例如this.PushPinLocation = new Geocoordinate(高度,经度,纬度)
-
如果您想在地图上显示多个点,您(显然)需要绑定到一组点。当您更改模型的属性(PushPinLocation..)时,它会更新视图,到目前为止是正确的。但是期望它只是添加一个新的子元素是错误的假设。如果您想要这种行为,您需要在数据源中有一个位置集合并绑定到该集合,以便所有位置显示在您的地图上。
标签: c# windows-phone-7 mvvm windows-phone-8 windows-phone