zoukankan      html  css  js  c++  java
  • 【WPF】奇怪的INotifyPropertyChanged的实现

    MSDN是这样解释的:

    INotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知。

    例如,考虑一个带有名为 FirstName 属性的 Person 对象。 若要提供一般性属性更改通知,则 Person 类型实现 INotifyPropertyChanged 接口并在 FirstName 更改时引发 PropertyChanged 事件。

    这一种大家都明白,实现方法是这样:

     

    public class Person : INotifyPropertyChanged
    {
        private string firstNameValue;
        public string FirstName{
            get { return firstNameValue; }
            set
            {
                firstNameValue=value;
                // Call NotifyPropertyChanged when the property is updated
                NotifyPropertyChanged("FirstName");
            }
        }
    
        // Declare the PropertyChanged event
        public event PropertyChangedEventHandler PropertyChanged;
    
        // NotifyPropertyChanged will raise the PropertyChanged event passing the
        // source property that is being updated.
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }  
    }

    不过我们可以把它封装起来,以便直接调用:我之前看到有两种实现方法,但不明白后者为什么要那样写,请各位指点:

    第一种:

    public class NotificationObject : INotifyPropertyChanged
     {
            public event PropertyChangedEventHandler PropertyChanged;
             private void RaisePropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
    }

    然后在我们自定义的类中使用:

    public class Person : NotificationObject
    {
        private string firstNameValue;
        public string FirstName{
            get { return firstNameValue; }
            set
            {
                firstNameValue=value;
                // Call NotifyPropertyChanged when the property is updated
                RaisePropertyChanged("FirstName");
            }
    }

    上面这一种经常使用,但下面这一种却不知道有什么好处,或者优点,用的是表达式树。

     

     public class NotificationObject : INotifyPropertyChanged
        {
            protected void RaisePropertyChanged<T>(Expression<Func<T>> action)
            {
                var propertyName = GetPropertyName(action);
                RaisePropertyChanged(propertyName);
            }
    
            private static string GetPropertyName<T>(Expression<Func<T>> action)
            {
                var expression = (MemberExpression)action.Body;
                var propertyName = expression.Member.Name;
                return propertyName;
            }
    
            private void RaisePropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }

    调用的时候也是直接调用:不过这次传入的不是属性参数,而是一个表达式。

    public class Person : NotificationObject
    {
        private string firstNameValue;
        public string FirstName{
            get { return firstNameValue; }
            set
            {
                firstNameValue=value;
                // Call NotifyPropertyChanged when the property is updated
                RaisePropertyChanged(()=>Name);
            }
    }

    请各位指点。

  • 相关阅读:
    隐藏win10中“此电脑”里的6个子文件夹
    k2安装LEDE
    ubuntu下用apt-get安装lamp缺少mcrypt , curl
    Devexpress TreeList选择父级联动
    iPhone10.3.X越狱后SSH连接不上的问题(已解决)
    C#保存Base64格式图片
    PHP服务器文件管理器开发小结(九):jQuery动态表单实现文件下载
    利用Content-disposition实现无刷新下载图片文件
    jQuery多文件下载
    参考资料
  • 原文地址:https://www.cnblogs.com/wywnet/p/3503778.html
Copyright © 2011-2022 走看看