我想在Bing地图上制作一个"汽车"点的动画。当物品四处移动时,我可以很容易地画出多个点,但我希望每辆车都能画出一个点。
XAML
<m:Map Name="myMap" Grid.Row="2" MouseClick="myMap_MouseClick" UseInertia="True">
<m:MapLayer x:Name="carLayer" />
</m:Map>
部分代码:
private void AddCarDot(double latitude, double longitude)
{
Ellipse point = new Ellipse();
point.Width = 15;
point.Height = 15;
point.Fill = new SolidColorBrush(Colors.Blue);
point.Opacity = 0.65;
Location location = new Location(latitude, longitude);
MapLayer.SetPosition(point, location);
MapLayer.SetPositionOrigin(point, PositionOrigin.Center);
carLayer.Children.Add(point);
}
private void cmbCar_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(cmbCar.SelectedItem != null)
{
Binding binding = new Binding("CarLocation");
binding.Source = cmbCar.SelectedItem;
binding.Mode = BindingMode.OneWay;
carLayer.SetBinding(MapLayer.PositionProperty, binding);
}
}
CarLocation是类型为Location的Car对象上的特性。然而,这不起作用,我不太确定如何让"汽车"在地图上移动。有人能给我指正确的方向吗?
当一个神秘的"taxiLayer"出现时,你的问题会变得模糊不清。当你想在它上设置一个绑定而不是"点"(我想它代表一辆车)时,它会变得泥泞。
需要做的是使用MapLayer.Position
依赖属性作为附加属性。当它所连接的UIElement是MapLayer
映射层的子级时,知道如何布局它
因此,问题是如何将绑定分配给该属性,以便在绑定对象的值更改时更新位置。我将假设在代码的前一部分中创建的Elipse作为字段可用,我将称之为car
。然后代码可能看起来像这样:-
private Elipse AddCarDot(object source)
{
Ellipse point = new Ellipse();
point.Width = 15;
point.Height = 15;
point.Fill = new SolidColorBrush(Colors.Blue);
point.Opacity = 0.65;
MapLayer.SetPositionOrigin(point, PositionOrigin.Center);
point.SetBinding(MapLayer.PositionProperty, new Binding("CarLocation") {Source = source});
carLayer.Children.Add(point);
}
private void cmbCar_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(cmbCar.SelectedItem != null)
{
AddCarDot(cmbCar);
}
}
现在假设具有CarLocation
属性的对象实现INotifyPropertyChanged
,以便在CarLocation
更改时提醒绑定,则点将适当移动。