通过我的Visual Commander扩展,您可以使用以下C#命令(参考:Microsoft.VisualBasic)来创建视图模型属性:
public class C : VisualCommanderExt.ICommand
{
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
string propertyName = Microsoft.VisualBasic.Interaction.InputBox("Property name", "Create view model property [1/2]", "Foo", -1, -1);
string propertyType = Microsoft.VisualBasic.Interaction.InputBox("Property type", "Create view model property [2/2]", "double", -1, -1);
string fieldName = "_" + System.Char.ToLower(propertyName[0]) + propertyName.Substring(1);
string snippet = @"
private {1} {2};
public {1} {0}
{{
get {{ return {2}; }}
set {{ SetProperty(ref {2}, value); }}
}}
";
EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
ts.Text = string.Format(snippet, propertyName, propertyType, fieldName);
}
}
使用C# 6更简单,但需要Visual Commander Professional:
public class C : VisualCommanderExt.ICommand
{
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
string propertyName = Microsoft.VisualBasic.Interaction.InputBox("Property name", "Create view model property [1/2]", "Foo", -1, -1);
string propertyType = Microsoft.VisualBasic.Interaction.InputBox("Property type", "Create view model property [2/2]", "double", -1, -1);
string fieldName = "_" + System.Char.ToLower(propertyName[0]) + propertyName.Substring(1);
EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
ts.Text = $@"
private {propertyType} {fieldName};
public {propertyType} {propertyName}
{{
get {{ return {fieldName}; }}
set {{ SetProperty(ref {fieldName}, value); }}
}}
";
}
}