首页 > 编程语言 >C# INotifyPropertyChanged Small Demo

C# INotifyPropertyChanged Small Demo

时间:2022-11-25 16:45:43浏览次数:45  
标签:INotifyPropertyChanged C# changed Called pc Small Property public Name


public class PChangeTest:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propName)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }
        private int _num;
        public int Num
        {
            get { return _num; }
            set
            {
                this._num = value;
                OnPropertyChanged("Num");

            }
        }
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }
    }

class Program
{
    public static void DealEventA(object sender, PropertyChangedEventArgs e)
        {
            Console.WriteLine($"Property {e.PropertyName} is changed!");
            Console.WriteLine("This is Called in DealEventA");
        }
        public static void DealEventB(object sender, PropertyChangedEventArgs e)
        {
            Console.WriteLine($"Again,I notice Property {e.PropertyName} is changed!");
            Console.WriteLine("This is Called in DealEventB");
        }
        static void Main()
        {
            PChangeTest pc = new PChangeTest();
            pc.PropertyChanged += DealEventA;
            pc.PropertyChanged += DealEventB;
            pc.Num = 3;
            pc.Name = "JohnYang";
         }
}

output:

Property Num is changed!
This is Called in DealEventA
Again,I notice Property Num is changed!
This is Called in DealEventB
Property Name is changed!
This is Called in DealEventA
Again,I notice Property Name is changed!
This is Called in DealEventB

标签:INotifyPropertyChanged,C#,changed,Called,pc,Small,Property,public,Name
From: https://www.cnblogs.com/johnyang/p/16925623.html

相关文章

  • SpringBoot集成ServiceComb Pack
    SpringBoot2.x集成ServiceCombpack事务基本概念​有过后端​​数据库​​编程经验的童鞋应该知道事务的基本理论知识同时网上有许多更为规范的文档参考,我在这里大致简单......
  • mac更换homebrew
    在mac系统中,使用homebrew可以很方便的管理包。按照官网的说明执行以下命令时总是报错: ​​​/usr/bin/ruby-e"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew......
  • Mac 安装Minikube
    环境信息:guoguo-MacBook-Pro-3:~guoguo$dockerversionClient: Version:   17.12.0-ce APIversion:   1.35 Goversion:   go1.9.2 Gitcommit:   c......
  • Lock接口
    packagecom.google;importjava.util.concurrent.locks.Lock;importjava.util.concurrent.locks.ReentrantLock;classDemo{privateinti=100;Lock......
  • conda 环境常用命令
    个人使用体验,conda环境非常容易出现问题,故非常不建议在base(默认环境)中使用,应新建环境,在新环境中使用 创建新环境名称为XXXpython版本号为3.7condacreate--nameXXX......
  • centos7安装rabbitmq3.9.16
    下载erlangwget--content-disposition--no-check-certificatehttps://packagecloud.io/rabbitmq/erlang/packages/el/7/erlang-23.3.4.11-1.el7.x86_64.rpm/download.......
  • 外观模式(Facade )
    就是封装!就是封装!就是封装!把一个很复杂的过程,包装成一个方法。调用的时候,只使用了一条语句,非常简介!非常美观! 比如:创建一个对象,只需要:new类名();它的内部......
  • 13、go的TCP网络编程
    创建客户端funcmain(){ //所需的网络编程全都在net包下 conn,err:=net.Dial("tcp","127.0.0.1:8888") fmt.Println("客户端启动") iferr!=nil{//连接失......
  • 装饰模式(Decorator)
    给某个对象,动态新增功能。构成:1、抽象接口:真实对象与装饰对象实现同一个接口。2、装饰父类:持有一个真实对象的引用,即可为真实对象新增功能。3、真实对象:被扩展的对象。......
  • Stream流Collectors.toMap用法
    packagecom.best.buc.verification.constant;importlombok.AllArgsConstructor;importlombok.Getter;importlombok.Setter;importjava.util.ArrayList;impo......