1

我想在 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 对象的一个​​属性。然而,这不起作用,我不太确定如何让“汽车”在地图上移动。有人可以指出我正确的方向吗?

4

1 回答 1

0

好吧,当您想在其上设置绑定而不是“点”(我猜它代表汽车)时,当一个神秘的“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

于 2011-06-16T21:47:10.227 回答