zoukankan      html  css  js  c++  java
  • 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));
           }
       }
    }
    

     

     
     
  • 相关阅读:
    Chapter 03Using SingleRow Functions to Customize Output(03)
    Chapter 03Using SingleRow Functions to Customize Output(01)
    Chapter 04Using Conversion Functions and Conditional ExpressionsNesting Functions
    Chapter 04Using Conversion Functions and Conditional ExpressionsGeneral Functions
    Chapter 11Creating Other Schema Objects Index
    传奇程序员John Carmack 访谈实录 (zz.is2120)
    保持简单纪念丹尼斯里奇(Dennis Ritchie) (zz.is2120.BG57IV3)
    王江民:传奇一生 (zz.is2120)
    2011台湾游日月潭
    2011台湾游星云大师的佛光寺
  • 原文地址:https://www.cnblogs.com/yanglang/p/9935302.html
Copyright © 2011-2022 走看看