WPF依赖项属性设计如何节省内存消耗? [英] How does the WPF dependency property design save memory consumption?

查看:92
本文介绍了WPF依赖项属性设计如何节省内存消耗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下链接中阅读了此内容:-

I read this in the following link:-

http://www.informit.com/articles/article.aspx?p=688529&seqNum=2


但是,由于GetValue和SetValue在内部使用了高效的稀疏存储系统,并且因为IsDefaultProperty是静态字段(而不是实例字段),所以与之相比,依赖项属性实现节省了每个实例的内存典型的.NET属性。如果WPF控件上的所有属性都是实例字段周围的包装器(就像大多数.NET属性一样),则由于附加到每个实例的所有本地数据,它们将消耗大量内存。

However, because GetValue and SetValue internally use an efficient sparse storage system and because IsDefaultProperty is a static field (rather than an instance field), the dependency property implementation saves per-instance memory compared to a typical .NET property. If all the properties on WPF controls were wrappers around instance fields (as most .NET properties are), they would consume a significant amount of memory because of all the local data attached to each instance.

但是最终它们被存储在某个地方,如何节省内存消耗?

But eventually they are getting stored somewhere, how does it saves memory consumption ?

推荐答案

请参阅以下链接: http://www.bobpowell .net / dependencyproperty.aspx


对象声明为依赖项属性实际上是
什么都没有不仅仅是标识符。这个静态属性实际上是一个
密钥,它将对象与特定的存储标识符相关联。对于
示例,图形对象具有Background属性,该属性可以显式设置
或通过使用模板或样式来设置。.

What is declared by an object as a dependency property is in fact nothing more than an identifier. This static "property" is really a key which associates an object with a specific storage identifier. For example graphical objects have a Background property that can be set explicitly or through the use of templates or styles..

只要Dependency Property使用其默认状态(这是很常见的),它就不会占用任何额外的内存,因为将使用默认值。默认值不是按实例存储,而是按依赖项属性存储,由元数据设置。

As long as a Dependency Property uses its default state (which is very common), it won't take up any additional memory since the default value will be used. The default value isn't stored per instance, it is stored per Dependency Property and it's set by metadata.

示例,请注意 Brushes.Black 设置为默认值

Example, notice how Brushes.Black is set as the default value

public static readonly DependencyProperty ForegroundProperty =
    DependencyProperty.Register(
        "Foreground",
        typeof(Brush),
        typeof(TextElement),
        new FrameworkPropertyMetadata(Brushes.Black, ...));

这样想:假设您有四个 TextBlocks

Think of it this way: Say you have four TextBlocks in Xaml

<StackPanel>
    <TextBlock .../>
    <TextBlock .../>
    <TextBlock .../>
    <TextBlock Foreground="Green" .../>
</StackPanel>

顶部的三个 TextBlocks 具有前景设置为 Black ,尽管您从未明确将其设置为 Black 。他们正在使用其默认值。因此,对于上面三个 TextBlocks 前景属性,您只需要一个字段(因为它是静态字段) 。

The three TextBlocks at the top have Foreground set to Black although you have never explicitly set it to Black. They are using their default value. So for the Foreground property for the three TextBlocks above, you only need one field (since it is a static field).

对于第四个 TextBlock ,您已经明确设置了前景到Green,因此该值作为此实例上 Foreground 的本地值插入到字典中,因此需要额外的内存(而且,它将结束在下面的列表中排在第3位,覆盖了 Setters Triggers 等)

For the forth TextBlock though, you have explicitly set Foreground to Green, so that value is inserted into a dictionary as the local value for Foreground on this instance and thus requires additional memory (also, it will end up at place number 3 in the list below, overriding Setters, Triggers etc).

另外,请参见Josh Smith的以下帖子,这是一本好书:解除依赖属性的神秘化

Also, see the following post by Josh Smith, it's a good read: Demystifying dependency properties


有一组定义明确的规则, WPF
在内部使用它来确定DP的实际价值。这是解析DP值时使用的优​​先级规则的简短摘要
(有关
的更多信息,请阅读
此处):


  1. 财产制度强制

  2. 活动动画或具有保持行为的动画

  3. 局部值

  4. TemplatedParent模板

  5. 样式触发器

  6. 模板触发器

  7. 样式设置器

  8. 主题样式

  9. 继承

  10. 依赖项属性元数据中的默认值

  1. Property system coercion
  2. Active animations, or animations with a Hold behavior
  3. Local value
  4. TemplatedParent template
  5. Style triggers
  6. Template triggers
  7. Style setters
  8. Theme style
  9. Inheritance
  10. Default value from dependency property metadata


编辑:要回答Duane的评论

To answer the comment from Duane

如果您将值明确设置为与默认值相同,则它将仍将存储为本地值。可以使用以下Xaml轻松验证。

If you explicitly set the value to the same value as the default value, it will still get stored as the local value. This can easily be verified with the following Xaml.

两个 TextBlocks 都会将前景设置为 Black ,但后者设置了局部值。 样式将只能在第一个 TextBlock 前景 c>,而不是后来的版本,因为样式设置器的优先级低于本地值。

Both TextBlocks will have Foreground set to Black, but the later has a local value set. The Style will only be able to set Foreground on the first TextBlock and not the later since Style setters has lower priority than local value.

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Green"/>
        </Style>
    </StackPanel.Resources>
    <TextBlock Text="Displays in Green"/>
    <TextBlock Foreground="Black" Text="Displays in Black"/>
</StackPanel>

这篇关于WPF依赖项属性设计如何节省内存消耗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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