C# 为什么“propdp”代码段不使用nameof运算符作为注册属性的名称?
如果插入代码段propdp,它不会在方法的第一个参数中使用nameof运算符作为属性名,它会创建如下内容:C# 为什么“propdp”代码段不使用nameof运算符作为注册属性的名称?,c#,visual-studio-2015,dependency-properties,code-snippets,C#,Visual Studio 2015,Dependency Properties,Code Snippets,如果插入代码段propdp,它不会在方法的第一个参数中使用nameof运算符作为属性名,它会创建如下内容: public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } // Using a DependencyProperty as the backing store for
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(""));
如果在下一个示例中使用运算符名称like,显然会更好:
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(""));
您可以按照以下步骤修改代码段: 找到代码段的文件。选择菜单选项“工具/代码段管理器…”。。。。将显示“代码段管理器”对话框。 在语言中,选择CSharp。 打开NetFX30并选择定义依赖项属性。您将在位置中看到文件的路径。应位于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$));
到
保存并记住以管理员身份打开文本编辑器
重新启动Visual Studio。nameof相对较新,我不确定代码片段是否能够确定您的项目也正在编译哪个版本的C。如果您同时处理旧的和新的c项目,而代码片段无法确定使用的是哪个版本的c,那么这可能就是原因之一。保持代码段支持旧代码段,以便无论发生什么情况,它都能正常工作,而不是在旧项目中有时被破坏。或者,它可能被忽略,或者使用nameof创建一个新的代码段,例如npropdp,以便在使用C 6的情况下使用。
public static readonly DependencyProperty $property$Property =
DependencyProperty.Register(nameof($property$) , typeof($type$), typeof($ownerclass$), new PropertyMetadata($defaultvalue$));