1010
1111namespace SharpPusher . MVVM
1212{
13- public class InpcBase : INotifyPropertyChanged
13+ /// <summary>
14+ /// Base (abstract) class implementing <see cref="INotifyPropertyChanged"/>.
15+ /// Could be used for both ViewModels and Models.
16+ /// </summary>
17+ public abstract class InpcBase : INotifyPropertyChanged
1418 {
1519 public InpcBase ( )
1620 {
1721 PropertyDependencyMap = new Dictionary < string , List < string > > ( ) ;
1822
19- foreach ( var property in GetType ( ) . GetProperties ( ) )
23+ foreach ( PropertyInfo property in GetType ( ) . GetProperties ( ) )
2024 {
21- var attributes = property . GetCustomAttributes < DependsOnPropertyAttribute > ( ) ;
22- foreach ( var dependsAttr in attributes )
25+ foreach ( DependsOnPropertyAttribute dependsAttr in property . GetCustomAttributes < DependsOnPropertyAttribute > ( ) )
2326 {
24- if ( dependsAttr == null )
27+ if ( dependsAttr is not null )
2528 {
26- continue ;
27- }
28-
29- foreach ( var dependence in dependsAttr . DependentProps )
30- {
31- if ( ! PropertyDependencyMap . ContainsKey ( dependence ) )
29+ foreach ( string dependence in dependsAttr . DependentProps )
3230 {
33- PropertyDependencyMap . Add ( dependence , new List < string > ( ) ) ;
31+ if ( ! PropertyDependencyMap . TryGetValue ( dependence , out List < string > ? value ) )
32+ {
33+ value = new List < string > ( ) ;
34+ PropertyDependencyMap . Add ( dependence , value ) ;
35+ }
36+
37+ value . Add ( property . Name ) ;
3438 }
35- PropertyDependencyMap [ dependence ] . Add ( property . Name ) ;
3639 }
3740 }
3841 }
3942 }
4043
4144
4245
43- /// <summary>
44- /// The PropertyChanged Event to raise to any UI object
45- /// </summary>
46- public event PropertyChangedEventHandler PropertyChanged ;
46+ private readonly Dictionary < string , List < string > > PropertyDependencyMap ;
4747
4848 /// <summary>
49- /// Dictonary of properties which have a dependant property.
49+ /// The PropertyChanged Event to raise to any UI object
5050 /// </summary>
51- protected readonly Dictionary < string , List < string > > PropertyDependencyMap ;
52-
51+ public event PropertyChangedEventHandler ? PropertyChanged ;
5352
5453
5554 /// <summary>
56- /// The PropertyChanged Event to raise to any UI object
55+ /// Raises the <see cref="PropertyChanged"/> event using the given property name.
5756 /// The event is only invoked if data binding is used
5857 /// </summary>
5958 /// <param name="propertyName">The Name of the property that is changing.</param>
6059 protected void RaisePropertyChanged ( string propertyName )
6160 {
62- PropertyChangedEventHandler handler = PropertyChanged ;
63- if ( handler != null )
61+ if ( PropertyChanged is not null )
6462 {
65- // Raise the PropertyChanged event.
66- handler ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
63+ PropertyChanged . Invoke ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
6764
68- // Raise the PropertyChanged event for dependant properties too .
69- if ( PropertyDependencyMap . ContainsKey ( propertyName ) )
65+ // Also raise the PropertyChanged event for dependant properties.
66+ if ( PropertyDependencyMap . TryGetValue ( propertyName , out List < string > ? value ) )
7067 {
71- foreach ( var p in PropertyDependencyMap [ propertyName ] )
68+ foreach ( string p in value )
7269 {
73- handler ( this , new PropertyChangedEventArgs ( p ) ) ;
70+ PropertyChanged . Invoke ( this , new PropertyChangedEventArgs ( p ) ) ;
7471 }
7572 }
7673 }
7774 }
7875
76+
7977 /// <summary>
80- /// Sets the value of a property.
81- /// <para/> Returs false if value didn't change.
78+ /// Sets the value of a property and raises the <see cref="PropertyChanged"/> event.
8279 /// </summary>
83- /// <typeparam name="T">Type of the field</typeparam>
84- /// <param name="field">Field to change value of</param>
85- /// <param name="value">New value to assign to the field</param>
86- /// <param name="propertyName">The Name of the property that is changing. If null it passes caller name in compile time.</param>
87- /// <returns>Returs false if value didn't change.</returns>
88- protected bool SetField < T > ( ref T field , T value , [ CallerMemberName ] string propertyName = null )
80+ /// <typeparam name="T">Type of the property</typeparam>
81+ /// <param name="field">Property's backing field to change</param>
82+ /// <param name="value">New value to set the <paramref name="field"/> to</param>
83+ /// <param name="propertyName">
84+ /// [Default value = null]
85+ /// The Name of the property that is changing. If it was null, the name is resolved at runtime automatically.
86+ /// </param>
87+ /// <returns>Retruns true if the value was changed, false if otherwise.</returns>
88+ protected bool SetField < T > ( ref T field , T value , [ CallerMemberName ] string ? propertyName = null )
8989 {
90- if ( EqualityComparer < T > . Default . Equals ( field , value ) )
90+ if ( EqualityComparer < T > . Default . Equals ( field , value ) || propertyName is null )
9191 {
9292 return false ;
9393 }
@@ -98,6 +98,5 @@ protected bool SetField<T>(ref T field, T value, [CallerMemberName] string prope
9898 return true ;
9999 }
100100 }
101-
102101 }
103102}
0 commit comments