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

        }

}

请各位指点。

你可能感兴趣的:(property)