wpf - 为什么将 DependencyProperty 成员声明为 public 不 protected ?

标签 wpf dependency-properties

为什么以这种方式创建 DependencyProperty 成员:

public static readonly DependencyProperty DepProperty = DependencyProperty.Register(...);

而不是那样:
protected static readonly DependencyProperty DepProp = DependencyProperty.Register(...);

当我们拥有 CLR“包装器”时,为什么我们需要从外部使用 DevProp 成员:
public bool Dep
{
    get { return (bool)GetValue(DepProperty); }
    set { SetValue(DepProperty, value); }
}

最佳答案

According to MSDN , 限制性访问修饰符实际上并没有为某些 API 提供预期的访问保护,因此除了 public 之外,声明依赖属性及其标识符字段没有任何意义。 :

Dependency Property Security Considerations

Dependency properties should be declared as public properties. Dependency property identifier fields should be declared as public static fields. Even if you attempt to declare other access levels (such as protected), a dependency property can always be accessed through the identifier in combination with the property system APIs. Even a protected identifier field is potentially accessible because of metadata reporting or value determination APIs that are part of the property system, such as LocalValueEnumerator. For more information, see Dependency Property Security.


将它们暴露为 public 不会造成任何伤害无论如何,我会聚集。

关于wpf - 为什么将 DependencyProperty 成员声明为 public 不 protected ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7818476/

相关文章:

c# - 派生类型是否也成为基类中定义的依赖属性的所有者(在 WPF/XAML 中)

android-studio - 无法构建 android apk 错误 : ZipException: duplicate entry: org/objectweb/asm/AnnotationVisitor. 类

wpf - 依赖属性如何保持值(value)

wpf - 验证错误模板未显示数据错误

c# - 为什么要在移动环境中使用 Entity Framework?

WPf、MEF 架构设计

c# - F# 默认值中的依赖属性不匹配

c# - ItemsControl 数据绑定(bind)不起作用?

c# - 在 WPF C# TreeView 中获取子节点的父节点

wpf - 在 xaml 中设置自定义窗口属性