10
10
11
11
namespace SharpPusher . MVVM
12
12
{
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
14
18
{
15
19
public InpcBase ( )
16
20
{
17
21
PropertyDependencyMap = new Dictionary < string , List < string > > ( ) ;
18
22
19
- foreach ( var property in GetType ( ) . GetProperties ( ) )
23
+ foreach ( PropertyInfo property in GetType ( ) . GetProperties ( ) )
20
24
{
21
- var attributes = property . GetCustomAttributes < DependsOnPropertyAttribute > ( ) ;
22
- foreach ( var dependsAttr in attributes )
25
+ foreach ( DependsOnPropertyAttribute dependsAttr in property . GetCustomAttributes < DependsOnPropertyAttribute > ( ) )
23
26
{
24
- if ( dependsAttr == null )
27
+ if ( dependsAttr is not null )
25
28
{
26
- continue ;
27
- }
28
-
29
- foreach ( var dependence in dependsAttr . DependentProps )
30
- {
31
- if ( ! PropertyDependencyMap . ContainsKey ( dependence ) )
29
+ foreach ( string dependence in dependsAttr . DependentProps )
32
30
{
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 ) ;
34
38
}
35
- PropertyDependencyMap [ dependence ] . Add ( property . Name ) ;
36
39
}
37
40
}
38
41
}
39
42
}
40
43
41
44
42
45
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 ;
47
47
48
48
/// <summary>
49
- /// Dictonary of properties which have a dependant property.
49
+ /// The PropertyChanged Event to raise to any UI object
50
50
/// </summary>
51
- protected readonly Dictionary < string , List < string > > PropertyDependencyMap ;
52
-
51
+ public event PropertyChangedEventHandler ? PropertyChanged ;
53
52
54
53
55
54
/// <summary>
56
- /// The PropertyChanged Event to raise to any UI object
55
+ /// Raises the <see cref="PropertyChanged"/> event using the given property name.
57
56
/// The event is only invoked if data binding is used
58
57
/// </summary>
59
58
/// <param name="propertyName">The Name of the property that is changing.</param>
60
59
protected void RaisePropertyChanged ( string propertyName )
61
60
{
62
- PropertyChangedEventHandler handler = PropertyChanged ;
63
- if ( handler != null )
61
+ if ( PropertyChanged is not null )
64
62
{
65
- // Raise the PropertyChanged event.
66
- handler ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
63
+ PropertyChanged . Invoke ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
67
64
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 ) )
70
67
{
71
- foreach ( var p in PropertyDependencyMap [ propertyName ] )
68
+ foreach ( string p in value )
72
69
{
73
- handler ( this , new PropertyChangedEventArgs ( p ) ) ;
70
+ PropertyChanged . Invoke ( this , new PropertyChangedEventArgs ( p ) ) ;
74
71
}
75
72
}
76
73
}
77
74
}
78
75
76
+
79
77
/// <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.
82
79
/// </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 )
89
89
{
90
- if ( EqualityComparer < T > . Default . Equals ( field , value ) )
90
+ if ( EqualityComparer < T > . Default . Equals ( field , value ) || propertyName is null )
91
91
{
92
92
return false ;
93
93
}
@@ -98,6 +98,5 @@ protected bool SetField<T>(ref T field, T value, [CallerMemberName] string prope
98
98
return true ;
99
99
}
100
100
}
101
-
102
101
}
103
102
}
0 commit comments