为什么"propdp"代码段不使用nameof运算符作为注册属性的名称?

jos*_*lmt 5 c# dependency-properties code-snippets visual-studio-2015

如果您插入片段propdp,它不会在DepencendyProperty.Register方法的第一个参数中使用nameof运算符作为属性名称,它会创建如下所示的内容:

    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(MyContentControl), new PropertyMetadata(""));
Run Code Online (Sandbox Code Playgroud)

如果你在下一个例子中使用运算符nameof,那么可能会更好:

    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(nameof(Text), typeof(string), typeof(MyContentControl), new PropertyMetadata(""));
Run Code Online (Sandbox Code Playgroud)

jos*_*lmt 8

您可以按照以下步骤修改代码段:

  • 找到代码段的文件.选择菜单选项工具/代码段管理器....将显示"代码片段管理器"对话框.
  • 语言中,选择CSharp.
  • 打开NetFX30并选择Define a Dependency Property.您将在位置中看到该文件的路径.应该在C:\ Program Files(x86)\ Microsoft Visual Studio 14.0\VC#\ Snippets\1033\NetFX30

打开文件并更改宏的定义

public static readonly DependencyProperty $property$Property = 
DependencyProperty.Register("$property$", typeof($type$), typeof($ownerclass$), new PropertyMetadata($defaultvalue$));
Run Code Online (Sandbox Code Playgroud)

public static readonly DependencyProperty $property$Property = 
DependencyProperty.Register(nameof($property$) , typeof($type$), typeof($ownerclass$), new PropertyMetadata($defaultvalue$));
Run Code Online (Sandbox Code Playgroud)

并保存(记得以管理员身份打开文本编辑器).

重新启动Visual Studio.