注册 DependencyProperty 时 'name' 参数的用途是什么?

What's the purpose of the 'name' argument when registering a DependencyProperty?

简单的问题。注册依赖项 属性 时 name 参数的用途是什么?我从未见过它在任何地方使用过,而且它似乎不必是唯一的。不太确定它的意义所在。

我一般都是这样设置然后就忘了:

public static readonly DependencyProperty SupportsPlayProperty = DependencyProperty.Register(
     nameof(SupportsPlay),
     typeof(bool),
     typeof(Player),
     new FrameworkPropertyMetadata(true));

public bool SupportsPlay {
    get => (bool)GetValue(SupportsPlayProperty);
    set => SetValue(SupportsPlayProperty, value);
}

当然我不能对附加属性执行此操作,因为它们没有任何与之配对的 CLR 属性(按照惯例)。我只是想包装 DependencyProperty 而不是 CLR 属性 (这意味着它会有额外的 'Property' 后缀)但是我 运行 变成了两个不同的附加的情况两个不同 类 中的属性被称为 'Enabled',这让我想知道 属性。

public static class ClearSelectionOnLostFocus {

    public static readonly DependencyProperty EnabledProperty = DependencyProperty.RegisterAttached(
        nameof(EnabledProperty),
        typeof(bool),
        typeof(ClearSelectionOnLostFocus),
        new FrameworkPropertyMetadata(false, ClearSelectionOnLostFocus_Changed));

    ...
}

那么这个名字有什么意义呢?

阅读 Microsoft docs on Custom Dependency Properties 让我相信这个值是内部使用的,必须遵循这个命名约定,

There are established naming conventions regarding dependency properties that you must follow...

That name must be unique within each registering type. ("SupportsPlay", the name we are discussing)

When you create the identifier field, name this field by the name of the property as you registered it, plus the suffix Property. (SupportsPlayProperty)

Again, by convention, the name of the wrapper property (public bool SupportsPlay) must be the same as the name chosen and given as first parameter of the Register call that registered the property.

并且可以在该页面中找到警告,

If your property does not follow the convention, this does not necessarily disable all possible uses, but you will encounter several notable issues...

Certain aspects of styles and templates will not work.

Most tools and designers must rely on the naming conventions to properly serialize XAML, or to provide designer environment assistance at a per-property level.

The current implementation of the WPF XAML loader bypasses the wrappers entirely, and relies on the naming convention when processing attribute values.