[英]Databinding of MapControl Center property in Windows Phone 8.1
我正在使用MVVM Light和使用MapControl的PCL开发Windows Phone 8.1(winRT)应用程序。 我在MapControl Center属性的绑定方面遇到问题。 在应用程序初始化时,在ViewModel中设置属性,并且地图正确居中。
但是,当我在ViewModel中更新值时,地图不会重新居中,但是如果我将值绑定到文本块,它将正确更新。
XAML:
<Maps:MapControl BorderThickness="2" BorderBrush="Black"
x:Name="Map"
HorizontalAlignment="Right" Margin="0,45,0,0"
VerticalAlignment="Top"
Height="595" Width="400"
ZoomLevel="10"
LandmarksVisible = "False"
TrafficFlowVisible = "False"
PedestrianFeaturesVisible = "False"
Center="{Binding Path=ViewStoreModel.CenterPosition, Mode=OneWay, Converter={StaticResource NormalizedAnchorPointConverter}}"
MapServiceToken="{StaticResource MapServiceTokenString}">
<Maps:MapItemsControl x:Name="MapIcons" ItemsSource="{Binding ViewStoreModel.ListStoreSearch}" >
<Maps:MapItemsControl.ItemTemplate>
<DataTemplate x:Name="Temp" >
<StackPanel Tapped="Image_Tapped" x:Name="MyStack" Maps:MapControl.Location="{Binding store_position, Converter={StaticResource GeoPointConvertCenter}}">
<Image x:Name="PinsImage" Source="ms-appx:///Assets/map-pin-button.png" />
</StackPanel>
</DataTemplate>
</Maps:MapItemsControl.ItemTemplate>
</Maps:MapItemsControl>
</Maps:MapControl>
ViewModel的属性:
public Location CenterPosition
{
get
{
return _centerPosition;
}
set
{
Set(CenterPositionPropertyName, ref _centerPosition, value);
}
}
public class Location : ObservableObject
{
public const string latitudePropertyName = "latitude";
public const string longitudePropertyName = "longitude";
private double _latitude;
private double _longitude;
public double latitude
{
get
{
return _latitude;
}
set
{
Set(latitudePropertyName, ref _latitude, value);
}
}
public double longitude
{
get
{
return _longitude;
}
set
{
Set(longitudePropertyName, ref _longitude, value);
}
}
该属性中心是Geopoint类型,因此我使用转换器将其从自定义类Location转换为它。 Center是一个依赖项属性,因此它应该是可绑定的。
谢谢你的帮助。
我发现MapControl中存在一些错误。 问题来自绑定模式。 OneWay似乎像OneTime(仅在init上)工作。 如果我将TwoWay放下,则它会起作用,但是地图会不断更新ViewModel。
作为一种解决方法,我指定必须明确告知XAML必须何时更新源。
码:
<Maps:MapControl BorderThickness="2" BorderBrush="Black"
x:Name="Map"
HorizontalAlignment="Right" Margin="0,45,0,0"
VerticalAlignment="Top"
Height="595" Width="400"
ZoomLevel="10"
LandmarksVisible = "False"
TrafficFlowVisible = "False"
PedestrianFeaturesVisible = "False"
Center="{Binding ViewStoreModel.CenterPosition, Mode=TwoWay, UpdateSourceTrigger=Explicit, Converter={StaticResource NormalizedAnchorPointConverter}}"
MapServiceToken="{StaticResource MapServiceTokenString}"
>
我们使用了一些不同的方法。
我们使用了DataContext的PropertyChanged
事件-在处理程序中,我们正在检查e.PropertyName
等于CurrentLocation
,如果确实如此,我正在调用NearbySitesMap.SetView(currentLocation, ZoomLevel, NearbySitesMap.Heading);
。
这样,当CurrentLocation更改时,地图将获得一个很好且平滑的动画到新位置。
您是否尝试删除“路径”属性并将其直接绑定到ViewModel?
<Maps:MapControl Center="{Binding ViewStoreModel.CenterPosition, Mode=OneWay, Converter={StaticResource NormalizedAnchorPointConverter}}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.