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);
            }
    }

    请各位指点。

  • 相关阅读:
    nginx 出现413 Request Entity Too Large问题的解决方法
    PHP中的插件机制原理和实例
    PHP扩展开发:第一个扩展
    阿拉伯语在网页中排版问题解决
    Piwik学习 -- 插件开发
    piwik高负载加速之切换session存储位置
    php 函数func_get_args()、func_get_arg()与func_num_args()之间的比较
    Docker: 基础介绍 [一]
    深入剖析Kubernetes学习笔记:开篇词(00)
    网络编程基础【林老师版】:简单的 套接字通信(一)
  • 原文地址:https://www.cnblogs.com/wywnet/p/3503778.html
Copyright © 2011-2022 走看看