using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HKW.HKWViewModels.SimpleObservable; /// /// 可观察值 /// /// public class ObservableValue : INotifyPropertyChanging, INotifyPropertyChanged where T : notnull { private T? _value = default; /// /// 当前值 /// public T? Value { get => _value; set { if (_value?.Equals(value) is true) return; PropertyChanging?.Invoke(this, new(nameof(Value))); ValueChanging?.Invoke(_value, value); _value = value; PropertyChanged?.Invoke(this, new(nameof(Value))); ValueChanged?.Invoke(value); } } /// public ObservableValue() { } /// /// 值 public ObservableValue(T value) { _value = value; } /// /// 属性改变前事件 /// public event PropertyChangingEventHandler? PropertyChanging; /// /// 属性改变后事件 /// public event PropertyChangedEventHandler? PropertyChanged; /// /// 值改变前事件 /// public event ValueChangingEventHandler? ValueChanging; /// /// 值改变后事件 /// public event ValueChangedEventHandler? ValueChanged; /// /// 值改变后事件方法 /// /// 值 public delegate void ValueChangedEventHandler(T? value); /// /// 值改变前事件方法 /// /// 旧值 /// 新值 public delegate void ValueChangingEventHandler(T? oldValue, T? newValue); }