问题描述
对我来说很清楚,为什么依赖项属性是静态的,而我仍然想知道的问题是为什么我们在声明依赖项属性时需要使用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.
推荐答案
从概念上讲,依赖项属性是依赖项对象仅具有的东西,与依赖项的使用时间无关。就像CLR属性一样,如果您询问此对象是否具有 Total
属性,则知道它不能是 double
现在,但 int
之后。结果,如果可以,我们将使依赖属性 const
成为可能,但我们不能这样做,因此 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 a double
now but an int
later. As a result, we'd make the dependency property const
if we could, but we cannot, so readonly
is the next best thing.
使用 readonly
关键字至少具有三个作用:
Using the readonly
keyword has at least three effects:
- 它告知读者代码值不会改变
- 它防止作者意外更改值
- 它有助于编译器,它受益于知道什么时候不会改变
这篇关于为什么Dependency属性被声明为静态只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!