使用Silverlight附加行为的Bing Map未获得更改的事件

时间:2012-06-20 18:23:01

标签: silverlight bing-maps attached-properties propertychanged

我想重绘我的Bing地图视图,放大一组点,但我不想使用map.SetView在后面的代码中完成,因为我想尽可能保持MVVM。我创建了一个对象(BoundingBox)来维护4个边界点(NorthLatitude,EastLongitude等),因此我可以创建在此语句中使用的LocationRect,否则我将在后面的代码中完成:     map.SetView(LocationRect.CreateLocationRect(X)); 我添加了一个附加属性,将值绑定到我在viewModel(MapBoundingBox)中维护的BoundingBox属性。问题是在我的页面初始加载后,我再也没有点击我附加属性的已更改事件。这是我的附属物:

public class MyInteractor : DependencyObject
{
    public static readonly DependencyProperty MyMapBoundingBoxProperty =
        DependencyProperty.RegisterAttached(
            "MyMapBoundingBox",
            typeof(BoundingBox),
            typeof(MyInteractor),
            new PropertyMetadata(null, OnMyMapBoundingBoxChanged));

    public static void SetMyMapBoundingBox(DependencyObject target, BoundingBox value)
    {
        target.SetValue(MyMapBoundingBoxProperty, value);
    }
    public static BoundingBox GetMyMapBoundingBox(DependencyObject target)
    {
        return (BoundingBox)target.GetValue(MyMapBoundingBoxProperty);
    }

    private static void OnMyMapBoundingBoxChanged(
        DependencyObject d,
        DependencyPropertyChangedEventArgs e)
    {
        var map = d as Microsoft.Maps.MapControl.Map;

        if (map == null)
            return;

        var bb = e.NewValue as BoundingBox;
        if (bb != null)
        {
            map.SetView(new Microsoft.Maps.MapControl.LocationRect(bb.NorthLatitude, bb.WestLongitude, bb.SouthLatitude, bb.EastLongitude));
        }
    }

我在xaml中包含我附加的属性:

<Grid>
    <m:Map ScaleVisibility="Visible" TabNavigation="Local" Mode="Road" 
           Center="{Binding MapCenter, Mode=TwoWay}"
           ZoomLevel="{Binding MapZoomLevel, Mode=TwoWay}"
           attachedProperties:MyInteractor.MyMapBoundingBox="{Binding MapBoundingBox, Mode=TwoWay}"               
        >

我的ViewModel属性(使用SetValue为“iNotifyProprtyChanged”):

    private BoundingBox _mapBoundingBox;
    public BoundingBox MapBoundingBox
    {
        get { return _mapBoundingBox; }
        set
        {
            if (SetValue(() => MapBoundingBox, value, ref _mapBoundingBox))
            {
                //do something else...
            }
        }
    }

虽然SetValue总是以虚假的方式返回,但不知何故它认为值没有改变,尽管我在调试中看到它正在改变。

我尝试使用一种行为很好的接受我无法弄清楚如何触发新attachProperty上的行为。这是我尝试过的行为,以及我如何在地图ViewChangedStart事件上实现搭载,虽然我不能把它留在那个事件上......

    public class MapAutoZoomBehavior : Behavior<msMap.Map>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        this.AssociatedObject.ViewChangeStart += OnViewChangeStart;
    }

    private void OnViewChangeStart(object sender, MapEventArgs e)
    {
        BoundingBox mapBoundingBox = new BoundingBox();
        mapBoundingBox.NorthLatitude = 33.80277;
        mapBoundingBox.SouthLatitude = 33.779161;
        mapBoundingBox.EastLongitude = -117.766647;
        mapBoundingBox.WestLongitude = -117.826984;
        AssociatedObject.SetView(new LocationRect(box.NorthLatitude, box.WestLongitude, box.SouthLatitude, box.EastLongitude));
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        this.AssociatedObject.ViewChangeStart -= OnViewChangeStart;
    }

        <i:Interaction.Behaviors>
            <pi:MapAutoZoomBehavior attachedProperties:MyInteractor.MyMapBoundingBox="{Binding MapBoundingBox, Mode=TwoWay}"></pi:MapAutoZoomBehavior>
           <!-- or   <pi:MapAutoZoomBehavior></pi:MapAutoZoomBehavior> -->
        </i:Interaction.Behaviors>

关于我做错的任何想法?谢谢!

0 个答案:

没有答案