为什么“propdp”代码片段没有建议声明依赖项属性的类的正确名称

本文关键字:属性 依赖 声明 propdp 代码 片段 为什么 | 更新日期: 2023-09-27 18:34:35

当您使用 propdp 代码片段创建依赖项属性时,它不会为您建议创建依赖项属性的类的正确名称,您必须手动键入它,如下一个示例所示:

namespace YourApp.Controls
{
    public sealed class YourButton : Control
    {
        public YourButton()
        {
            this.DefaultStyleKey = typeof(YourButton);
        }
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(ownerclass), new PropertyMetadata(0));
    }
}

我不想作为默认值所有者类,在这种情况下我想要您的按钮

如何修改代码片段以建议正确的名称?

为什么“propdp”代码片段没有建议声明依赖项属性的类的正确名称

分析ctor代码片段的源代码,很容易知道问题所在:你只需要添加下一行:

<Function>ClassName()</Function>

在文本所有者类的定义中。

  • 打开文件 C:''Program Files (x86(''Microsoft Visual Studio 14.0''VC#''Snippets''1033''NetFX30''propdp.snippet。
  • 在所有者类的定义中添加该行。
  • 保存文件。
  • 重新启动 Visual Studio。

该文件必须如下所示:

...
<Literal>
    <ID>ownerclass</ID>
    <ToolTip>The owning class of this Property.  Typically the class that it is declared in.</ToolTip>
    <Function>ClassName()</Function>
    <Default>ownerclass</Default>
</Literal>
...

然后,默认情况下,您将拥有所需的内容:

namespace YourApp.Controls
{
    public sealed class YourButton : Control
    {
        public YourButton()
        {
            this.DefaultStyleKey = typeof(YourButton);
        }
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(YourButton), new PropertyMetadata(0));
    }
}

使用为什么"propdp"代码片段不使用 nameof 运算符作为注册属性的名称?中建议的修改,使其使用 nameof 运算符。