为什么 WPF 中的依赖属性必须是静态的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19441453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:53:07  来源:igfitidea点击:

Why dependency properties in WPF has to be Static

c#.netwpfc#-4.0dependency-properties

提问by Learner

Why a dependency property has to be Static?

为什么依赖属性必须是静态的?

I have seen that it has been already asked in some post here, but I am not able to understand it properly.

我已经看到这里的一些帖子已经问过了,但我无法正确理解它。

It will be great if someone can help me understand with a small snippet too.

如果有人也能用一个小片段帮助我理解,那就太好了。

回答by Nitin

The magic here is, the declaration of DependencyPropertyis staticnot its value(i.e the memory storage). The declaration that you add with statickeyword is just the identifier(key) of the DependencyPropertyfor a particular DependencyObject. As same identifier/key can be used by all instances of the DependencyObject to identify the property value hence it makes sense to make it static.

这里神奇的是,声明DependencyPropertystatic不是它value(即存储器)。您使用static关键字添加的声明只是DependencyProperty特定DependencyObject. 由于 DependencyObject 的所有实例都可以使用相同的标识符/键来标识属性值,因此使它成为有意义的static

On the other hand, when we set the value of DependancyPropertyby calling the SetValueon DependancyObjectinstance, then each instance of DependancyObjecton which the SetValue is called will store its local value of the Property. This is handled internally by the DependancyObjectclass which maintain sort of Dictionarywhich has the mapping between the DependancyPropertyidentifier and the local value.

另一方面,当我们DependancyProperty通过调用SetValueonDependancyObject实例设置 的值时,则调用DependancyObjectSetValue 的每个实例都会存储其本地的 Property 值。这由DependancyObject维护某种类型的类在内部处理,该类Dictionary具有DependancyProperty标识符和本地值之间的映射。

回答by Narendra Singh

DependencyProperty has to be static (Class level)because when we create multiple objects of the class which has that property and want to refer the default value for that property the value has to come from that static instance of DependencyProperty. So default value for all instance of our class is same and system does not reserve memory for DependencyProperty on each and every instance of that class. This way it reduces the memory footprint.

DependencyProperty 必须是静态的(类级别),因为当我们创建具有该属性的类的多个对象并希望引用该属性的默认值时,该值必须来自 DependencyProperty 的静态实例。因此,我们类的所有实例的默认值都是相同的,并且系统不会在该类的每个实例上为 DependencyProperty 保留内存。这样可以减少内存占用。

Now the next question arise what if we explicitly set the DependencyProperty's value for objects of the class.(By code or by animation or by style)

现在出现下一个问题,如果我们为类的对象显式设置 DependencyProperty 的值会怎样。(通过代码或动画或样式)

In this case DependencyObjectcomes into the picture. Any class which has DependencyProperty has to be derived from DependencyObject class (WPF specific class which maintains a collection named EffectiveValues). When user set the DependencyProperty's value explicitlyon the object of the class(By code or by animation or by style), the value is stored in the that EffectiveValues collection which resides within the DependencyObject class and reserve memory there.

在这种情况下,DependencyObject就出现了。任何具有 DependencyProperty 的类都必须从 DependencyObject 类(维护名为 EffectiveValues集合的WPF 特定类)派生。当用户在类的对象上显式设置 DependencyProperty 的值时(通过代码、动画或样式),该值存储在位于 DependencyObject 类中的 EffectiveValues 集合中,并在那里保留内存。