Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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(""));
like image 740
joseangelmt Avatar asked Feb 14 '16 13:02

joseangelmt


1 Answers

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.

like image 156
joseangelmt Avatar answered Oct 05 '22 01:10

joseangelmt