InotifyPropertyChange接口的使用
仅代表个人学习时的观点,如有错误,欢迎指出
想让属性拥有通知ui值已经改变的能力,属性的所有者必须得实现InotifyPropertyChange接口
class Class1 : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private int amount; public int Amount { set { if (value == amount) return; amount = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Amount")); } get { return amount; } } }
这样在触发PropertyChanged事件后,ui就会得知值已经改变了,但这有些繁琐
可以在nuget下载PropertyChanged.Fody这个包

代码就可以简化成如下
[AddINotifyPropertyChangedInterface] class Class1 : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public int Amount { set; get; }//必须是属性 字段不行 }

浙公网安备 33010602011771号