简体   繁体   English

silverlight、C#,为 XAML 添加额外控制

[英]silverlight, C#, adding extra control for XAML

As many bing maps silverlight developers already know, bing maps contains a method called setview that calculates the right zoomlevel for the map.正如许多 bing maps silverlight 开发人员已经知道的那样,bing maps 包含一个名为 setview 的方法,该方法为 map 计算正确的缩放级别。

I myself built a map applicion with an MVVM framework.我自己用 MVVM 框架构建了一个 map 应用程序。 I want to use the setview method also for my map.我也想对我的 map 使用 setview 方法。 But because i built the app in an MVVM, it won't be good to use setview in the viewmodel, because the viewmodel doesn't know anything about the map.xaml但是因为我在MVVM中构建了应用程序,所以在viewmodel中使用setview并不好,因为viewmodel对map.xaml一无所知

I have an XAML with the map UI.我有一个 XAML 和 map UI。 I connected the XAML to a viewmodel, called mapcontent.cs.我将 XAML 连接到一个名为 mapcontent.cs 的视图模型。

In the viewmodel called, mapcontent.cs, i have a property like this:在名为 mapcontent.cs 的视图模型中,我有一个这样的属性:

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

Now i want to use the setview, but this with the MVVM set up.现在我想使用 setview,但这与 MVVM 设置有关。 So i created an extra control of the Map class:所以我创建了 Map class 的额外控制:

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
  }
}

now the solution will be, that i can use ViewArea in my XAML and bind that with MapArea.现在解决方案是,我可以在我的 XAML 中使用 ViewArea 并将其与 MapArea 绑定。 The only problem is that i can't use the property Viewarea in XAML, does anyone know why?唯一的问题是我不能使用 XAML 中的属性 Viewarea,有人知道为什么吗?

Your ViewArea property would have to be a DependencyProperty in order to support bindings.您的 ViewArea 属性必须是DependencyProperty才能支持绑定。 What you have is a plain old CLR property.你所拥有的是一个普通的旧 CLR 属性。

To hook up the SetView call, you'd have to add a change handler to your dependency property.要连接 SetView 调用,您必须将更改处理程序添加到您的依赖项属性。 This article has an example/explaination, but it would be like:这篇文章有一个例子/解释,但它会像:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM