Implementing INotifyPropertyChanged - does a better way exist?

前端 未结 30 2679
感情败类
感情败类 2020-11-21 05:23

Microsoft should have implemented something snappy for INotifyPropertyChanged, like in the automatic properties, just specify {get; set; notify;} I

30条回答
  •  死守一世寂寞
    2020-11-21 05:52

    I keep this around as a snippet. C# 6 adds some nice syntax for invoking the handler.

    // INotifyPropertyChanged
    
    public event PropertyChangedEventHandler PropertyChanged;
    
    private void Set(ref T property, T value, [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer.Default.Equals(property, value) == false)
        {
            property = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

提交回复
热议问题