DependencyProperty作为实例变量? [英] DependencyProperty as instance-variables?

查看:53
本文介绍了DependencyProperty作为实例变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管大多数网络上的代码示例都使用DependencyProperties的静态声明,但在某些情况下,我发现它们被定义为公共只读实例成员.

While most code samples around the net use a static declaration of DependencyProperties, I found in some cases they are defined as public readonly instance members.

将DependencyProperty定义为实例成员(不是静态的)是否有优点?

Are there any pros in defining a DependencyProperty as an instance-member (and not static)?

(注意:我不想讨论静态成员是好是坏的问题,而是更具体地讲DependencyProperty对象的行为)

(Note: I dont want to get in the question of whether or not static members are good or bad, rather more specifically on the behaviour of the DependencyProperty object)

推荐答案

那不是一个好主意.一旦控件的实例不止一个,则应引发异常,因为再次注册了同一属性,并且该注册是按类型而不是按实例的.

That is not quite a good idea. As soon as there is more than one instance of the control an exception should be thrown as the same property is being registered again and the registration is per-type and not per-instance.

"X"属性已由"Y"注册.

'X' property was already registered by 'Y'.

在您遇到的情况下,可能是编码员的疏忽.

Possibly a slip by the coder in the cases you encountered.

这篇关于DependencyProperty作为实例变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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