WPF属性系统如何经济?

发布于 2024-10-30 02:15:07 字数 69 浏览 3 评论 0原文

据说WPF的设计者已经让它变得经济或者性能更高了。有人可以用一个例子来解释一下幕后发生的事情使 WPF 属性系统更加经济吗?

It is said that the designers of WPF have made it economical or higher performance. Can someone please explain with an example of what happens under the hood that makes the WPF property system more economical?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

比忠 2024-11-06 02:15:07

您可能指的是依赖属性比普通 CLR 属性“便宜”这一事实。

简而言之:

依赖属性是使用稀疏数据结构实现的,这些数据结构仅在属性值设置在对象上时才为其分配内存。相反,标准 CLR 属性值作为字段存储在定义该属性的类的每个对象中,即使所有这些对象都将该属性设置为其默认值。

例如,如果我们有 100 个对象,每个对象都有 100 个 int 类型的 CLR 属性,那么我们将使用 10000 个 int 的内存,即使所有这些对象都具有相同的内存默认值 (0)。

如果该属性是依赖属性,则我们根本不会使用任何额外的内存:WPF 不需要记住任何属性的值,因为它知道您没有更改默认值。

当然,上面是一个相当简单的解释,并没有涵盖依赖属性相对于 CLR 属性的所有优点,但它应该充分解释了“DP 具有更高的性能”这一说法。

You are probably referring to the fact that Dependency Properties are "cheaper" than normal CLR properties.

In a few words:

Dependency properties are implemented using sparse data structures that only allocate memory for the property value if it is set on an object. In contrast, a standard CLR property value is stored as a field inside every object of the class in which the property is defined, even if all of these objects have the property set to its default value.

So for example, if we have 100 objects with 100 CLR properties of type int each, then we are using 10000 ints worth of memory even if all of those have the same default value (0).

If the property were a dependency property instead, we would not be using any additional memory at all: WPF does not need to remember the value of any property since it knows that you didn't change it from the default.

Of course the above is rather a simplistic explanation and does not cover all the advantages of dependency properties over CLR properties, but it should explain the "DPs have higher performance" statement adequately.

如日中天 2024-11-06 02:15:07

WPF 控件的大多数“属性”实际上并不存在于控件本身上。他们没有向(基)类添加数十个(大部分未使用的)属性,而是选择添加一个“属性包”,即一个仅保存实际设置的属性的字典。

作为奖励,它允许环境和注入属性。

Most of the 'properties' of a WPF control are in fact not present on the Control itself. Instead of adding dozens of (mostly unused) properties to the (base-)classes, they elected to add a "property bag" instead, a Dictionary holding only the properties that are actually set.

As a bonus it allows for ambient and injected properties.

内心旳酸楚 2024-11-06 02:15:07

WPF 依赖属性系统在后台将实际属性值存储在优化的数据结构中。

与将属性值存储为字段相比,这有几个优点:

  • 依赖属性系统通过不存储每个对象实例的属性的默认值,可以节省内存分配(基本上,如果属性具有目标对象的默认值,则空间不为值分配。这与始终存储值的属性相反,并且内存始终为对象保留。

  • 依赖属性系统可以具有优化的事件机制,避免在每个对象的基础上存储处理程序引用(例如使用基于支持字段的事件),这意味着可以节省更多空间。

当然,这样的系统有一点开销。属性访问并不像使用普通属性那样轻量级,但 WPF 团队认为,由于内存使用量较低,因此较小的开销足以弥补这一点。

The WPF dependency property system stores actual property values in optimized data structures behind the scenes.

This has several advantages over storing property values as fields:

  • The dependency property system by NOT storing default values for properties for each object instance it can save allot of memory (basically if a property has a default value for a target object, space is not allocated for the value. This is opposed to the having properties with backing fields where the values are always stored, and the memory is always reserved for the object).

  • The dependency property system can have optimized event mechanism that avoids storing handler references on a per object basis (like using backing field based events), this means more space savings can be made.

There is of course a small overhead for such a system. Property access is not as lightweight as using a normal property, but the WPF team has decided the small overhead more than makes it up due to lower memory usage.

扛刀软妹 2024-11-06 02:15:07

除了其他答案:

WPF 中的依赖属性支持属性值继承。对于普通的 CLR 属性,在不修改子对象的情况下将值下推到任何“子”对象要困难得多。显然,这可以通过附加方法和静态映射来完成,但可能不是一个非常通用的解决方案。虽然继承属性会产生一些开销,但它们在传递值方面相当有效。

In addition to the other answers:

Dependency properties in WPF support property value inheritance. With normal CLR properties it is much harder to push values down to any "child" objects without modifying the child object. This can obviously be done with attached methods and a static mapping, but would probably not be a very generic solution. While there is some overhead with inherited properties, they are fairly efficient at passing down values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文