为什么“propdp”代码段在您声明依赖属性的地方没有提出正确的类名?

时间:2016-02-15 13:33:12

标签: c# visual-studio visual-studio-2015 code-snippets

使用 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));
    }
}

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

如何修改代码段以提出正确的名称?

1 个答案:

答案 0 :(得分:1)

分析 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));
    }
}

使用Why "propdp" code snippet doesn't use the nameof operator for the name of the registered property?中建议的修改,使用 nameof 运算符。

相关问题