对 DependencyObject 和 DependencyProperty 的依赖 [英] Dependence on DependencyObject and DependencyProperty

查看:32
本文介绍了对 DependencyObject 和 DependencyProperty 的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 Silverlight 应用程序,我上次的警告之一是,如果您需要以 Silverlight/WPF 方式正确完成任何事情,您需要将您的对象建模为 DependecyObject 并使用 DependencyProperty(ies)

我发现这个模型相当麻烦,在我使用的类中需要静态字段和初始化器,所以使用旧的事件驱动(观察者模式?)代替 DependencyObject 是个好主意吗?

我的目标是尽量减少代码膨胀和样板(我讨厌它们),并且真的很想知道是否有在 Silverlight/WPF 方面有经验的人有任何提示/技术可以将 DependencyObject 和 DependencyProperty 的使用保持在最低限度?

这是个好主意吗?

解决方案

实际上,在 Silverlight 中,您不能继承 DependencyObjects,因此您应该(并且必须)改为实现 INotifyPropertyChanged.

实现 INotifyPropertyChanged 比 DependencyObjects(为了更容易)和使用 DependencyProperties (DP) 有很多优点:

  • 这个更轻量
  • 让您可以更自由地为对象建模
  • 可以轻松序列化
  • 您可以在需要时引发事件,这在某些情况下很有用,例如,当您只想在一个 UI 操作中捆绑多个更改时,或者当您需要引发事件时即使数据没有更改(强制重绘...)

另一方面,在 WPF 中继承 DO 具有以下优点:

  • 更容易实施,尤其适合初学者.
  • 您可以获得(几乎)免费的回调机制,允许您在属性值更改时收到通知
  • 您将获得一种强制机制,允许您为属性的最大值、最小值和现值定义规则.

还有其他考虑因素,但这些是主要考虑因素.

我认为普遍的共识是 DP 非常适合控件(您甚至可以在 Silverlight 中使用自定义 DP 实现 CustomControl),但对于数据对象,您应该实现 INotifyPropertyChanged.

HTH,洛朗

I'm building a Silverlight application and one of my caveats from last time was that if you need anything done right in Silverlight/WPF way you'd need to model your objects as a DependecyObject and use DependencyProperty(ies)

I find this model to be rather cumbersome, requiring static fields and initializers in half the classes I use, so is it a good idea to use the good-old event-driven (observer pattern?) in place of DependencyObject?

I'm aiming to minimize code bloat and boiler plates (I hate them) and really would like to know if anyone with experience in Silverlight/WPF has any tips/techniques for keeping usage of DependencyObject and DependencyProperty to a minimum?

Is this a good idea?

解决方案

Actually, in Silverlight you cannot inherit DependencyObjects, and so you should (and have to) implement INotifyPropertyChanged instead.

Implementing INotifyPropertyChanged has many advantages over DependencyObjects (I will abbreviate this DO to make it easier) and using DependencyProperties (DPs):

  • This is more lightweight
  • Allows you more freedom in modeling your objects
  • Can be serialized easily
  • You can raise the event when you want, which can be useful in certain scenarios, for example when you want to bundle multiple changes in only one UI operation, or when you need to raise the event even if the data didn't change (to force redraw...)

On the other hand, inheriting DOs in WPF have the following advantages:

  • Easier to implement especially for beginners.
  • You get a callback mechanism (almost) for free, allowing you to be notified when the property value changes
  • You get a coercion mechanism with allows you to define rules for max, min and present value of the property.

There are other considerations, but these are the main.

I think the general consensus is that DPs are great for controls (and you can implement a CustomControl with custom DPs even in Silverlight), but for data objects you should rather implement INotifyPropertyChanged.

HTH, Laurent

这篇关于对 DependencyObject 和 DependencyProperty 的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆