WPF开发进阶 - Fody/PropertyChanged(一)

INotifyPropertyChanged

在WPF MVVM模式开发中,实现INotifyPropertyChanged的ViewModel是非常重要且常见的类:

public class MainViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

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

在类里,需要响应变化的属性都需要在Setter里调用属性变化的方法:

private string _appName;
public string AppName
{
    get
    {
        return _appName;
    }
    set
    {
        if (_appName == value)
            return;
        _appName = value;
        OnPropertyChanged(nameof(AppName));
    }
}

这样的写法,一个两个属性还好,在有很多属性的情况下,就显得有一些繁杂,今天要介绍一款开源的工具就是为了解决这个问题。

Fody/PropertyChanged

开源地址: https://github.com/Fody/PropertyChanged/

在编译时,在属性中实现 INotifyPropertyChanged 的代码

使用方法

  1. 通过Nuget安装

    PM> Install-Package PropertyChanged.Fody

  2. 安装完成后,会在项目中,添加FodyWeavers.xml文件,这是Fody的配置文件,详细可以参考 Fody

<?xml version="1.0" encoding="utf-8"?>
<Weavers>
  <PropertyChanged />
</Weavers>

说明

项目中所有标记有[ImplementPropertyChanged]特性,或者实现了INotifyPropertyChanged的类,都将自动在其属性中实现通知相关的代码,除非属性被显示标记为不通知

  • 程序中的代码:
[ImplementPropertyChanged]
public class Person 
{        
    public string GivenNames { get; set; }
    public string FamilyName { get; set; }

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", GivenNames, FamilyName);
        }
    }
}

或者

public class MainViewModel : INotifyPropertyChanged
{

   public string GivenNames { get; set; }
   public string FamilyName { get; set; }

   public string FullName
   {
       get
       {
           return string.Format("{0} {1}", GivenNames, FamilyName);
       }
   }
   
   public event PropertyChangedEventHandler PropertyChanged;

   private void OnPropertyChanged(string propertyName)
   {
       if (this.PropertyChanged != null)
           this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
   }
}
  • 编译后对应的
public class Person : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;

   string givenNames;
   public string GivenNames
   {
       get { return givenNames; }
       set
       {
           if (value != givenNames)
           {
               givenNames = value;
               OnPropertyChanged("GivenNames");
               OnPropertyChanged("FullName");
           }
       }
   }

   string familyName;
   public string FamilyName
   {
       get { return familyName; }
       set 
       {
           if (value != familyName)
           {
               familyName = value;
               OnPropertyChanged("FamilyName");
               OnPropertyChanged("FullName");
           }
       }
   }

   public string FullName
   {
       get
       {
           return string.Format("{0} {1}", GivenNames, FamilyName);
       }
   }

   public virtual void OnPropertyChanged(string propertyName)
   {
       var propertyChanged = PropertyChanged;
       if (propertyChanged != null)
       {
           propertyChanged(this, new PropertyChangedEventArgs(propertyName));
       }
   }
}
posted @ 2017-02-01 19:12  重庆肥猫  阅读(14717)  评论(0编辑  收藏  举报