相信全部學wpf的,都寫過相似下面的代碼:
實現INotifyPropertyChangedgit
public class MainViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
調用github
private string _userName = string.Empty; /// <summary> /// 用戶名 /// </summary> public string UserName { get => _userName; set { _userName = value; OnPropertyChanged(); } }
當屬性多起來時,這就很煩人了····
因而乎,咱們的PropertyChanged.Fody
就登場了微信
這是一個附加組件庫。咱們能夠經過nuget安裝,也能夠經過在程序包管理控制檯輸入如下內容:網站
PM> Install-Package Fody PM> Install-Package PropertyChanged.Fody
安裝完成後,咱們須要手動添加名爲FodyWeavers.xml的文件,右鍵項目添加項選擇xml文件便可。
注:該文件是Fody配置文件,更多信息請參考配置
若是僅僅實現通知,咱們只須要在文件內添加一下內容this
<Weavers> <PropertyChanged/> </Weavers>
完成以上操做後,全部實現 INotifyPropertyChanged
的類都將通知代碼注入到屬性設置器中。
例如:code
public class Person : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public string GivenNames { get; set; } public string FamilyName { get; set; } public string FullName => $"{GivenNames} {FamilyName}"; }
在編譯後就會成爲:xml
public class Person : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; string givenNames; public string GivenNames { get => givenNames; set { if (value != givenNames) { givenNames = value; OnPropertyChanged(InternalEventArgsCache.GivenNames); OnPropertyChanged(InternalEventArgsCache.FullName); } } } string familyName; public string FamilyName { get => familyName; set { if (value != familyName) { familyName = value; OnPropertyChanged(InternalEventArgsCache.FamilyName); OnPropertyChanged(InternalEventArgsCache.FullName); } } } public string FullName => $"{GivenNames} {FamilyName}"; protected void OnPropertyChanged(PropertyChangedEventArgs eventArgs) { PropertyChanged?.Invoke(this, eventArgs); } } internal static class InternalEventArgsCache { internal static PropertyChangedEventArgs FamilyName = new PropertyChangedEventArgs("FamilyName"); internal static PropertyChangedEventArgs FullName = new PropertyChangedEventArgs("FullName"); internal static PropertyChangedEventArgs GivenNames = new PropertyChangedEventArgs("GivenNames"); }
咱們天然有些特殊需求,例如我須要更新A屬性通知B屬性,須要某些屬性不通知等等需求。因而Fody給咱們提供了標記特性。圖片
咱們只須要在屬性上打上要通知的屬性便可。get
public class Person : INotifyPropertyChanged { [AlsoNotifyFor("FullName")] public string GivenName { get; set; } [AlsoNotifyFor("FullName")] public string FamilyName { get; set; } public event PropertyChangedEventHandler PropertyChanged; public string FullName { get; set; } }
咱們也能夠標記某屬性更新時不須要通知。博客
public class Person : INotifyPropertyChanged { public string GivenName { get; set; } [DoNotNotify] public string FamilyName { get; set; } public event PropertyChangedEventHandler PropertyChanged; }
public class Person : INotifyPropertyChanged { public string GivenName { get; set; } public string FamilyName { get; set; } public event PropertyChangedEventHandler PropertyChanged; [DependsOn("GivenName","FamilyName")] public string FullName { get; set; } }
本人不是大佬,只是道路先行者,在落河後,向後來的人大喊一聲,這裏有坑,不要過來啊!
縱然如此,依舊有人重複着落河,重複着吶喊······
我的博客網站 Blog
技術交流Q羣: 1012481075 羣內有各類流行書籍資料
文章後續會在公衆號更新,微信搜索 OneByOneDotNet 便可關注。
你的一分鼓勵,個人十分動力,點贊免費,感恩回饋。喜歡就點贊評論吧,雙擊66~