为什么依赖属性被声明为静态只读?

发布于 2024-10-31 06:51:52 字数 64 浏览 1 评论 0原文

我很清楚为什么依赖属性是静态的,但我仍然想到的问题是为什么我们需要在声明依赖属性时使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

罪歌 2024-11-07 06:51:52

从概念上讲,依赖属性是依赖对象仅具有的东西,并且在使用该属性时不依赖于该属性。就像 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 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.

Using the readonly keyword has at least three effects:

  • it informs readers of the code that the value will not change
  • it prevents the author from accidentally changing the value
  • it assists the compiler, which benefits from knowing when things will not change
浴红衣 2024-11-07 06:51:52

因为很明显,该属性的值在初始化后不能更改。

Because it makes it obvious, that the value of this property cannot be changed after initialization.

烟酉 2024-11-07 06:51:52

希望这会有所帮助:Silverlight.net 论坛:DependencyProperty - 公共静态只读?

引用:

“public static readonly”是从 Register 调用返回的字段。该字段是属性的标识符。您实际上只需要标识符,以便 Silverlight 属性系统知道要做什么,并且您可以在定义依赖项属性的 CLR“包装器”时自己使用属性系统。一旦有了包装器,对该属性的所有进一步使用都可以像典型属性一样使用它。

公开,以便所有属性系统调用(包括跨程序集)都可以访问它。

静态和只读,因为这不是一个应该改变的定义;财产系统需要获得一致的结果。

在附加属性的情况下,您希望有一个“所有者”类。所有者类必须是调用 RegisterAttached 的类,并且还必须定义静态访问器方法(Get* 和 Set*),以便 XAML 解析器知道当您尝试在 DependencyObject 实例上设置附加属性时该怎么做。所以它有点不同,因为对于附加属性通常没有“包装器”,任何代码访问都只使用 Get* 和 Set* 访问器。

Hopefully this would help: Silverlight.net forums: DependencyProperty - public static readonly?

To quote:

The "public static readonly" is the field that comes back from the Register call. The field is the identifier for the property. You only really need the identifier so that the Silverlight property system knows what to do, and so that you can use the property system yourself as you define the dependency property's CLR "wrapper". Once you have the wrapper, all further usage of the property can just use it like a typical property.

Public so that all property system calls including cross-assembly can access it.

Static and readonly because this isn't a definition that should ever change; the property system needs to get consistent results.

In the attached property case, you want there to be an "owner" class. The owner class must be the class that calls RegisterAttached, AND must also define the static accessor methods (Get* and Set*) so that the XAML parser knows what to do when you try to set the attached property on a DependencyObject instance. So it's a little different, because for an attached property there usually isn't a "wrapper", any code access just uses the Get* and Set* accessors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文