c# - 在 wpf 中快速生成 ViewModel 属性?

标签 c# wpf mvvm

看完this article ,在我的 PersonViewModel 类中有以下代码:

public Jurisdiction CountryResidence
{
    get
    {
        return Model.CountryResidence;
    }
    set
    {
        if (Model.CountryResidence == value)
            return;
        else
        {
            Model.CountryResidence = value;
            base.OnPropertyChanged("CountryResidence");
        }
    }
}

public Jurisdiction CountryBirth
{
    get
    {
        return Model.CountryBirth;
    }
    set
    {
        if (Model.CountryBirth == value)
            return;
        else
        {
            Model.CountryBirth = value;
            base.OnPropertyChanged("CountryBirth");
        }
    }
}

我还有 CountryDomiciledCountryPassportLegalJurisdiction,它们都具有相同的格式。同样,我有很多 String 属性,所有这些属性都共享它们的格式。

这会导致很多相同的代码!但是,我不知道如何使它更简洁。

是否有更好的方法来生成这些属性以保持它们的强类型化?

最佳答案

我使用 Visual Studio 的代码片段,它为我生成带有后备存储和事件引发的属性。只需创建名为 propchanged(或其他名称,如果您愿意)和以下内容的 xml 文件:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>propchanged</Title>
      <Shortcut>propchanged</Shortcut>
      <Description>Code snippet for property (with call to OnPropertyChanged) and backing field</Description>
      <Author>lazyberezovsky</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>type</ID>
          <ToolTip>Property type</ToolTip>
          <Default>string</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 
    {
       if ($field$ == value)
          return;

       $field$ = value;
       OnPropertyChanged("$property$");
    }
    }
    $end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

并将其放入文件夹 C:\Users\YourName\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets\

接下来,我从一些基本 ViewModel 继承了我的 ViewModel,它实现了 INotifyPropertyChanged 接口(interface)并提供了用于生成“PropertyChanged”事件的 protected 方法 OnPropertyChanged

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName);
    }
}

现在,当您在 Visual Studio 中键入 propchanged 时,它会要求您提供属性类型和名称,并为您生成代码。

public class PersonViewModel : ViewModel
{
    // type here 'propchanged' (or other shortcut assigned for snippet)
}

更新:

另一种选择是通过 AOP 框架生成代码,例如 PostSharp .在这种情况下,代码将在编译期间生成并添加(因此您的类将保持干净)。 Here是通过 PostSharp 属性更改的实现 INotifyProperty 的示例:

[Notify]
public class PersonViewModel
{
    public Jurisdiction CountryResidence { get; set; }
    public Jurisdiction CountryBirth { get; set; }
}

关于c# - 在 wpf 中快速生成 ViewModel 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10174539/

相关文章:

c# - 在 WPF 中处理来自 c++ dll 的未处理异常

.net - WPF TreeView - 使用嵌套集合和 "Static nodes"绑定(bind)到 ViewModel

c# - wpf mvvm设置焦点数据网格的特定单元格

c# - WPF 将命令绑定(bind)到 Datagrid 内的 Datacontext

c# - log4net 日志记录还有哪些其他替代方案?

c# - MySql 上的 Entity Framework 4.4.0 代码优先迁移不断将 dbo 添加到新表

c# - 从另一个线程更改 viewmodel 属性

c# - 警告 - 在项目文件中找不到文件 'Views\file.xaml' 的父文件 'Views\file.xaml.cs'

c# - DataGridViewAutoSizeColumnMode.ColumnHeader 调整行头为2行

c# - 有没有办法用另一个库解压缩 DynaZip Max 文件? F.E. DotNetZip