如何自动创建WPF Viewmodel属性

时间:2017-05-02 16:29:08

标签: c# wpf visual-studio resharper prism

我在WPF项目中使用Visual Studio '17和Resharper,它向用户显示了大量信息。因此,我有很多属性如下所示。

private double _foo;
public double Foo
{
   get { return _foo; }
   set { SetProperty(ref _foo, value); }
}

我正在寻找一种快速自动化的方式来创建它。现在我输入'prop'并得到这个

public double Foo {get; set;}

点击'Alt-Enter' - > '到具有支持字段的财产'来获得此

private double _foo;
public double Foo
{
   get { return _foo; }
   set { _foo = value); }
}

然后手动输入SetProperty(ref和','以获得最终结果。我已经尝试设置片段和resharper模板,但似乎无法弄清楚如何在一个操作中执行此操作。人们想出来让这更容易吗?具体来说,如果我可以将已经存在的自动属性更改为View Model属性,那将会非常有用。

4 个答案:

答案 0 :(得分:2)

您可以定义自己的代码段,以精确满足您的需求。

这是我自己编写的一个,用于生成与我同时使用的INotifyPropertyChanged基类实现兼容的属性实现:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>INPCProperty</Title>
      <Author>Peter Duniho</Author>
      <Description>Property declaration for NotifyPropertyChangedBase</Description>
      <Shortcut>inpcprop</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>fieldName</ID>
          <Default>_fieldName</Default>
          <ToolTip>Enter actual field name here</ToolTip>
        </Literal>
        <Literal>
          <ID>fieldType</ID>
          <Default>object</Default>
          <ToolTip>Enter actual field value type here</ToolTip>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[private $fieldType$ $fieldName$;
        public $fieldType$ FieldName
        {
            get { return $fieldName$; }
            set { _UpdateField(ref $fieldName$, value); }
        }]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

使用Visual Studio Snippet Manager将代码段添加到您的安装中。

(我没有费心为属性名称添加字段,因为它似乎没有任何意义。但是,当然,如果你愿意,你可以。我看到的唯一优势就是选择属性名称和过度输入,如果它是一个字段,你应该能够使用tab键直接过度输入。)

答案 1 :(得分:0)

从Visual Studio Marketplace安装Prism Template Pack,然后使用&#34; propp&#34;片段。

https://marketplace.visualstudio.com/items?itemName=BrianLagunas.PrismTemplatePack

答案 2 :(得分:0)

您可以为此创建自己的代码段。我已经为BindableBase(来自PRISM)属性创建了propbb片段,它根本就是你想要的。要在此创建代码段,我已经关注了视频。

https://www.youtube.com/watch?v=tGmXlUFMTpk

我的代码片段看起来像这样

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>propbb</Title>
      <Shortcut>propbb</Shortcut>
      <Description>Code snippet for BindableBase property and backing field</Description>
      <Author></Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>type</ID>
          <ToolTip>Property type</ToolTip>
          <Default>int</Default>
        </Literal>
        <Literal>
          <ID>property</ID>
          <ToolTip>Property name</ToolTip>
          <Default>MyProperty</Default>
        </Literal>
        <Literal>
          <ID>field</ID>
          <ToolTip>The variable backing this property</ToolTip>
          <Default>myVar</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[private $type$ $field$;
                   public $type$ $property$
                  {
                    get { return $field$;}
                      set { SetProperty(ref $field$ , value);}
                  }
    $end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

如果你想要这是我的片段,只需将它包含在你的视觉工作室中(就像我提供的视频一样)

propbb snippet download

在VS2017中添加代码段的路径: C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ Professional \ VC#\ Snippets \ 1033 \ Visual C# 只需从我的保管箱下载代码段并在那里复制,然后使用它来编写propbb和标签页

答案 3 :(得分:0)

使用我的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); }}
}}
";
    }
}
相关问题