【问题标题】:Bing map silverlight binding for moving target用于移动目标的 Bing 地图 Silverlight 绑定
【发布时间】:2011-06-16 21:05:10
【问题描述】:

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

【问题讨论】:

    标签: silverlight binding bing


    【解决方案1】:

    好吧,当您想在其上设置绑定而不是“点”(我猜它代表汽车)时,当一个神秘的“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 更改时可以提醒绑定,点将适当移动。

    【讨论】:

      猜你喜欢
      • 2014-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多