[英]PostSharp & classes that (are supposed to) implement INotifyPropertyChanged
[英]How to implement INotifyPropertyChanged for derived classes?
我有一個基類:
public class PersonBaseClass : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
if (value != name)
{
name = value;
NotifyPropertyChanged("Name");
}
}
}
}
和派生類
public class TeacherClass : PersonBaseClass, INotifyPropertyChanged
{
private string id;
public string Id
{
get { return id; }
set
{
if (value != id)
{
id = value;
NotifyPropertyChanged("Id");
}
}
}
}
而這個神奇的代碼在每個上面的結尾!
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
然后我在xaml的列表中顯示Teachers
集合列表。 現在,如果我更改Id
,將為用戶顯示更改,但不會顯示作為基類屬性的Name
中的更改。 在調試中,我看到在設置Name
值后, NotifyPropertyChanged
方法內的handler
為null,這似乎是問題所在。
如何解決基類更改也會出現在列表中?
只有PersonBaseClass實現INotifyPropertyChanged並使NotifyPropertyChange成為受保護的,因此您可以從子類中調用它。 不需要兩次實現它。 這也應該解決問題。
您的“魔術代碼”部分應該只在PersonBaseClass中。 您可以使NotifyPropertyChanged函數受到保護,以便也可以從TeacherClass調用相同的函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.