使用CallerMemberName简化InotifyPropertyChanged的实现

2017-05-29 19:57:43来源:CSDN作者:qq_15807167人点击

在WPF中,当我们需要通过MVVM的方法绑定一个普通对象的属性时候,我们的界面需要获取属性的变更通知。因此需要实现InotifyPropertyChanged接口的实现。

class NotifyObject : INotifyPropertyChanged    {        private int number;        public int Number        {            get { return number; }            set { number = value; OnPropertyChanged("Number"); }        }        private string text;        public string Text        {            get { return text; }            set { text = value; OnPropertyChanged("Text"); }        }        public event PropertyChangedEventHandler PropertyChanged;        protected void OnPropertyChanged(string propertyName = "")        {            PropertyChangedEventHandler handler = PropertyChanged;            if (handler != null)            {                handler(this, new PropertyChangedEventArgs(propertyName));            }        }    }

从上面的代码中可以看到我们的属性名,是通过硬编码的方式进行传递,如果项目变更或重构就要进行更改对应的参数,因此我们可以通过CallerMemberName的 注入方式进行管理。

  class NotifyObject : INotifyPropertyChanged    {        private int number;        public int Number        {            get { return number; }            set { number = value; OnPropertyChanged(); }        }        private string text;        public string Text        {            get { return text; }            set { text = value; OnPropertyChanged(); }        }        public event PropertyChangedEventHandler PropertyChanged;        protected void OnPropertyChanged([CallerMemberName]string propertyName = "")        {            PropertyChangedEventHandler handler = PropertyChanged;            if (handler != null)            {                handler(this, new PropertyChangedEventArgs(propertyName));            }        }    }

或者进行更一步的封装,避免相同参数,重复更新界面。

/// <summary>    /// Observable object with INotifyPropertyChanged implemented    /// </summary>    public class ObservableObject : INotifyPropertyChanged    {        /// <summary>        /// Sets the property.        /// </summary>        /// <returns><c>true</c>, if property was set, <c>false</c> otherwise.</returns>        /// <param>Backing store.</param>        /// <param>Value.</param>        /// <param>Property name.</param>        /// <param>On changed.</param>        /// <typeparam>The 1st type parameter.<peparam>        protected bool SetProperty<T>(            ref T backingStore, T value,            [CallerMemberName]string propertyName = "",            Action onChanged = null)        {            if (EqualityComparer<T>.Default.Equals(backingStore, value))                return false;            backingStore = value;            onChanged?.Invoke();            OnPropertyChanged(propertyName);            return true;        }        /// <summary>        /// Occurs when property changed.        /// </summary>        public event PropertyChangedEventHandler PropertyChanged;        /// <summary>        /// Raises the property changed event.        /// </summary>        /// <param>Property name.</param>        protected void OnPropertyChanged([CallerMemberName]string propertyName = "")        {            var changed = PropertyChanged;            if (changed == null)                return;            changed.Invoke(this, new PropertyChangedEventArgs(propertyName));        }    }

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台