Why “propdp” code snippet doesn't use the nameof operator for the name of the registered property?

走远了吗. 提交于 2019-12-10 14:19:09

问题


If you insert the snippet propdp, it doesn't use the nameof operator for the property name in the first parameter of the DepencendyProperty.Register method and it creates something like this:

    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(""));

and obviusly can be better if you use the operator nameof like in the next example:

    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(""));

回答1:


You can modify the code snippet following the next steps:

  • Locate the snippet's file. Select menu option Tools/Code Snippets Manager.... The Code Snippets Manager dialog box will show.
  • In Language, select CSharp.
  • Open NetFX30 and select Define a Dependency Property. You'll see the path of the file in Location. Should be in C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\NetFX30

Open the file and change the definition of the macro from

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

to

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

and save (remember to open your text editor as an administrator).

Restart Visual Studio.



来源:https://stackoverflow.com/questions/35392084/why-propdp-code-snippet-doesnt-use-the-nameof-operator-for-the-name-of-the-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!