【问题标题】:Why “propdp” code snippet doesn't propose the correct name of the class in where you're declaring the dependency property?为什么“propdp”代码片段没有在您声明依赖属性的地方提出正确的类名称?
【发布时间】:2016-02-15 13:33:11
【问题描述】:

当您使用 propdp 代码 sn-p 创建依赖属性时,它不会为您提供创建依赖属性的类的正确名称,并且您ve 像在下一个示例中那样手动键入它:

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

我不希望 ownerclass 作为默认值,在这种情况下我希望 YourButton

如何修改代码 sn-p 以提出正确的名称?

【问题讨论】:

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


【解决方案1】:

分析ctor代码sn-p的源码其实很容易知道问题所在:只需要加上下一行:

<Function>ClassName()</Function>

在字面 ownerclass 的定义中。

  • 打开文件 C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\NetFX30\propdp.sn-p。
  • 在所有者类的定义中添加该行。
  • 保存文件。
  • 重新启动 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 运算符。

【讨论】:

  • 我觉得不用重启VS,试试这个。
  • 经过测试。您必须重新启动 VS。
猜你喜欢
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-07
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多