2011-06-29 61 views
1

bing地图包含一个名为setview的方法,用于计算地图的右缩放级别。silverlight,C#,为XAML添加额外控件

我自己用MVVM框架构建了一个地图应用程序。我想为我的地图使用setview方法。但是因为我在MVVM中构建了应用程序,所以在视图模型中使用setview并不好,因为viewmodel不知道任何有关map.xaml的内容。 我将XAML连接到一个名为mapcontent.cs的视图模型。

在叫,mapcontent.cs的视图模型,我有一个属性是这样的:

public LocationRect MapArea { 
    get { return new LocationRect(new Location(52.716610, 6.921160), new Location(52.718330, 6.925840)); } 
} 

现在我想使用的setView,但是这与MVVM成立。 所以我创建的地图类的额外控制:

namespace Markarian.ViewModels.Silverlight.Controls { 
    /// <summary> 
    /// Map control class 
    /// </summary> 
    public class Map: MC.Map { 
    /// <summary> 
    /// Initializes a new instance of the Map class. 
    /// </summary> 
    public Map() { 

    } 

    /// <summary> 
    /// gets and sets setview. 
    /// </summary> 
    public MC.LocationRect ViewArea { get; set; } <<<setview will come here 
    } 
} 

现在的解决方案将是,我可以在我的XAML中使用ViewArea并绑定与MapArea。 唯一的问题是,我不能在XAML中使用属性Viewarea,有谁知道为什么?

回答

0

您的ViewArea属性必须是DependencyProperty才能支持绑定。你有什么是一个普通的旧CLR属性。

要挂钩SetView调用,您必须将更改处理程序添加到您的依赖项属性。这article有一个例子/解释,但它会像:

public MC.LocationRect ViewArea { 
    get { return (MC.LocationRect)GetValue(ViewAreaProperty); } 
    set { SetValue(ViewAreaProperty, value); } 
} 

public static readonly DependencyProperty ViewAreaProperty = DependencyProperty.Register("ViewArea", typeof(MC.LocationRect), 
    typeof(Map), new PropertyMetadata(new MC.LocationRect(), OnViewAreaChanged)); 

private static void OnViewAreaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { 
    var map = d as Map; 
    // Call SetView here 
}