From 8a4d312b6a53c07054b24750d16b0ea6428093f6 Mon Sep 17 00:00:00 2001 From: CodingEnthusiast Date: Tue, 24 Dec 2024 11:13:26 +0330 Subject: [PATCH] Improve InpcBase --- Src/Denovo/MVVM/InpcBase.cs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Src/Denovo/MVVM/InpcBase.cs b/Src/Denovo/MVVM/InpcBase.cs index ef746966..4ad97585 100644 --- a/Src/Denovo/MVVM/InpcBase.cs +++ b/Src/Denovo/MVVM/InpcBase.cs @@ -31,11 +31,13 @@ public InpcBase() foreach (string dependence in dependsAttr.DependentProps) { - if (!PropertyDependencyMap.ContainsKey(dependence)) + if (!PropertyDependencyMap.TryGetValue(dependence, out List? value)) { - PropertyDependencyMap.Add(dependence, new List()); + value = new List(); + PropertyDependencyMap.Add(dependence, value); } - PropertyDependencyMap[dependence].Add(property.Name); + + value.Add(property.Name); } } } @@ -48,7 +50,7 @@ public InpcBase() /// /// The PropertyChanged Event to raise to any UI object /// - public event PropertyChangedEventHandler PropertyChanged; + public event PropertyChangedEventHandler? PropertyChanged; /// @@ -58,17 +60,16 @@ public InpcBase() /// The Name of the property that is changing. protected void RaisePropertyChanged(string propertyName) { - PropertyChangedEventHandler handler = PropertyChanged; - if (handler != null) + if (PropertyChanged is not null) { - handler(this, new PropertyChangedEventArgs(propertyName)); + PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); // Also raise the PropertyChanged event for dependant properties. - if (PropertyDependencyMap.ContainsKey(propertyName)) + if (PropertyDependencyMap.TryGetValue(propertyName, out List? value)) { - foreach (string p in PropertyDependencyMap[propertyName]) + foreach (string p in value) { - handler(this, new PropertyChangedEventArgs(p)); + PropertyChanged.Invoke(this, new PropertyChangedEventArgs(p)); } } } @@ -86,9 +87,9 @@ protected void RaisePropertyChanged(string propertyName) /// The Name of the property that is changing. If it was null, the name is resolved at runtime automatically. /// /// Retruns true if the value was changed, false if otherwise. - protected bool SetField(ref T field, T value, [CallerMemberName] string propertyName = null) + protected bool SetField(ref T field, T value, [CallerMemberName] string? propertyName = null) { - if (EqualityComparer.Default.Equals(field, value)) + if (EqualityComparer.Default.Equals(field, value) || propertyName is null) { return false; }