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

請各位指點。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM