为什么以这种方式创建 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/