为什么依赖属性被声明为静态只读?
我很清楚为什么依赖属性是静态的,但我仍然想到的问题是为什么我们需要在声明依赖属性时使用 Readonly 关键字。
It is clear to me why dependency property are static and the question still remain on my mind is why we need to use Readonly keyword at the time of declaration of Dependency Property.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从概念上讲,依赖属性是依赖对象仅具有的东西,并且在使用该属性时不依赖于该属性。就像 CLR 属性一样,如果你问这个对象是否有
Total
属性,你就知道它现在不能是double
,而是以后是int
。因此,如果可以的话,我们会将依赖属性设为const
,但我们不能,因此readonly
是下一个最好的选择。使用
readonly
关键字至少有三个作用:Conceptually a dependency property is something that a dependency object simply has and that does not depend on when you use the property. Just like a CLR property, if you ask does this object have a
Total
property, you know it cannot be adouble
now but anint
later. As a result, we'd make the dependency propertyconst
if we could, but we cannot, soreadonly
is the next best thing.Using the
readonly
keyword has at least three effects:因为很明显,该属性的值在初始化后不能更改。
Because it makes it obvious, that the value of this property cannot be changed after initialization.
希望这会有所帮助:Silverlight.net 论坛:DependencyProperty - 公共静态只读?
引用:
Hopefully this would help: Silverlight.net forums: DependencyProperty - public static readonly?
To quote: