问题
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