C#中INotifyPropertyChanged的实践

382 阅读1分钟

这是一个简单的实践,本来是要通过窗体来模拟实验的,那样更明显。但是我用控制台程序来模拟了。 定义类代码

 // This is a simple customer class that 
    // implements the IPropertyChange interface.
    public class DemoCustomer : INotifyPropertyChanged
    {
        // These fields hold the values for the public properties.
        private Guid idValue = Guid.NewGuid();
        private string customerNameValue = string.Empty;
        private string phoneNumberValue = string.Empty;

        public event PropertyChangedEventHandler PropertyChanged;

        // This method is called by the Set accessor of each property.
        // The CallerMemberName attribute that is applied to the optional propertyName
        // parameter causes the property name of the caller to be substituted as an argument.
        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            Console.WriteLine($"我是改变的数据{propertyName}");
            if (PropertyChanged != null)
            {
                //https://zhuanlan.zhihu.com/p/489899222
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            
            }
        }

        // The constructor is private to enforce the factory pattern.
        private DemoCustomer()
        {
            customerNameValue = "默认值--Customer";
            phoneNumberValue = "默认值--(312)555-0100";
        }

        // This is the public factory method.
        public static DemoCustomer CreateNewCustomer()
        {
            return new DemoCustomer();
        }

        // This property represents an ID, suitable
        // for use as a primary key in a database.
        public Guid ID
        {
            get
            {
                return this.idValue;
            }
        }

        public string CustomerName
        {
            get
            {
                return this.customerNameValue;
            }

            set
            {
                if (value != this.customerNameValue)
                {
                    this.customerNameValue = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public string PhoneNumber
        {
            get
            {
                return this.phoneNumberValue;
            }

            set
            {
                if (value != this.phoneNumberValue)
                {
                    this.phoneNumberValue = value;
                    NotifyPropertyChanged();
                }
            }
        }
    }

测试代码

            DemoCustomer px = DemoCustomer.CreateNewCustomer();
            Console.WriteLine(px.CustomerName);
            Console.WriteLine(px.PhoneNumber);
            px.PropertyChanged += (e,s)=> { //发生在修改值之前
                Console.WriteLine("查看属性");
                Console.WriteLine(e);//对象
                Console.WriteLine(s);//事件参数
            };
           px.CustomerName = "我是新值8888888"; //触发事件
            Console.WriteLine(px.CustomerName);
            Console.WriteLine(px.PhoneNumber);
            px.PropertyChanged += (e, s) => {
                Console.WriteLine("不会调用");
                Console.WriteLine(e);//对象
                Console.WriteLine(s);//事件参数
            };

运行结果图:

image.png

结论: 给字段重新赋值会触发PropertyChanged事件,从而做出一些操作。