@@ -89,117 +89,109 @@ protected void LoadFrom(string file)
89
89
90
90
if ( File . Exists ( file ) )
91
91
{
92
- try
92
+ Dispatcher . Invoke ( ( ) =>
93
93
{
94
- Dispatcher . Invoke ( ( ) =>
94
+ try
95
95
{
96
- try
97
- {
98
- JsonConvert . PopulateObject ( File . ReadAllText ( file ) , this ) ;
99
- }
100
- catch ( Exception e )
101
- {
102
- _logger . Exception ( e ) ;
103
- }
104
- } ) ;
96
+ JsonConvert . PopulateObject ( File . ReadAllText ( file ) , this ) ;
97
+ }
98
+ catch ( Exception e )
99
+ {
100
+ _logger . Exception ( e ) ;
101
+ }
102
+ } ) ;
103
+ }
105
104
106
- foreach ( var propertyInfo in properties )
105
+ foreach ( var propertyInfo in properties )
106
+ {
107
+ //Check if property is an observable collection
108
+ if ( typeof ( INotifyCollectionChanged ) . IsAssignableFrom ( propertyInfo . PropertyType ) )
109
+ {
110
+ //_logger.Debug($"Property {propertyInfo.Name} is an INotifyCollectionChanged");
111
+ //Set list changed event to trigger on property change
112
+ if ( propertyInfo . GetValue ( this ) is INotifyCollectionChanged collection )
107
113
{
108
- //Check if property is an observable collection
109
- if ( typeof ( INotifyCollectionChanged ) . IsAssignableFrom ( propertyInfo . PropertyType ) )
114
+ if ( propertyInfo . PropertyType . IsGenericType && typeof ( INotifyPropertyChanged ) . IsAssignableFrom ( propertyInfo . PropertyType . GenericTypeArguments [ 0 ] ) )
110
115
{
111
- //_logger.Debug($"Property {propertyInfo.Name} is an INotifyCollectionChanged ");
112
- //Set list changed event to trigger on property change
113
- var collection = propertyInfo . GetValue ( this ) as INotifyCollectionChanged ;
114
- if ( collection != null )
116
+ //_logger.Debug($"Property {propertyInfo.Name} is an INotifyPropertyChanged with generic type {propertyInfo.PropertyType.GenericTypeArguments[0]} ");
117
+
118
+ //loop through all items in collection and add property changed event
119
+ foreach ( var item in ( IEnumerable < object > ) collection )
115
120
{
116
- if ( propertyInfo . PropertyType . IsGenericType && typeof ( INotifyPropertyChanged ) . IsAssignableFrom ( propertyInfo . PropertyType . GenericTypeArguments [ 0 ] ) )
121
+ if ( item is INotifyPropertyChanged notifyPropertyChanged )
117
122
{
118
- //_logger.Debug($"Property {propertyInfo.Name} is an INotifyPropertyChanged with generic type {propertyInfo.PropertyType.GenericTypeArguments[0]}");
123
+ notifyPropertyChanged . PropertyChanged += OnNotifyPropertyChangedOnPropertyChanged ;
124
+ }
125
+ }
119
126
120
- //loop through all items in collection and add property changed event
121
- foreach ( var item in ( IEnumerable < object > ) collection )
127
+ collection . CollectionChanged += ( _ , args ) =>
128
+ {
129
+ OnPropertyChanged ( propertyInfo . Name ) ;
130
+
131
+ if ( args is { Action : NotifyCollectionChangedAction . Add , NewItems : not null } )
132
+ {
133
+ foreach ( var item in args . NewItems )
122
134
{
123
135
if ( item is INotifyPropertyChanged notifyPropertyChanged )
124
136
{
125
137
notifyPropertyChanged . PropertyChanged += OnNotifyPropertyChangedOnPropertyChanged ;
126
138
}
127
139
}
140
+ }
128
141
129
- collection . CollectionChanged += ( _ , args ) =>
142
+ if ( args is { Action : NotifyCollectionChangedAction . Remove , OldItems : not null } )
143
+ {
144
+ foreach ( var item in args . OldItems )
130
145
{
131
- OnPropertyChanged ( propertyInfo . Name ) ;
132
-
133
- if ( args is { Action : NotifyCollectionChangedAction . Add , NewItems : not null } )
134
- {
135
- foreach ( var item in args . NewItems )
136
- {
137
- if ( item is INotifyPropertyChanged notifyPropertyChanged )
138
- {
139
- notifyPropertyChanged . PropertyChanged += OnNotifyPropertyChangedOnPropertyChanged ;
140
- }
141
- }
142
- }
143
-
144
- if ( args is { Action : NotifyCollectionChangedAction . Remove , OldItems : not null } )
146
+ if ( item is INotifyPropertyChanged notifyPropertyChanged )
145
147
{
146
- foreach ( var item in args . OldItems )
147
- {
148
- if ( item is INotifyPropertyChanged notifyPropertyChanged )
149
- {
150
- notifyPropertyChanged . PropertyChanged -= OnNotifyPropertyChangedOnPropertyChanged ;
151
- }
152
- }
148
+ notifyPropertyChanged . PropertyChanged -= OnNotifyPropertyChangedOnPropertyChanged ;
153
149
}
154
- } ;
155
-
156
- void OnNotifyPropertyChangedOnPropertyChanged ( object ? o , PropertyChangedEventArgs eventArgs )
157
- {
158
- OnPropertyChanged ( propertyInfo . Name ) ;
159
150
}
160
151
}
161
- else
162
- {
163
- collection . CollectionChanged += ( _ , _ ) => { OnPropertyChanged ( propertyInfo . Name ) ; } ;
164
- }
165
- }
166
- else
167
- {
168
- _logger . Error ( $ "Property { propertyInfo . Name } is not an INotifyCollectionChanged it is { propertyInfo . PropertyType } ") ;
169
- }
170
- }
152
+ } ;
171
153
172
- if ( typeof ( IBindingList ) . IsAssignableFrom ( propertyInfo . PropertyType ) )
173
- {
174
- //Set list changed event to trigger on property change
175
- var collection = propertyInfo . GetValue ( this ) as IBindingList ;
176
- if ( collection != null )
177
- {
178
- collection . ListChanged += ( _ , _ ) => { OnPropertyChanged ( propertyInfo . Name ) ; } ;
179
- }
180
- else
154
+ void OnNotifyPropertyChangedOnPropertyChanged ( object ? o , PropertyChangedEventArgs eventArgs )
181
155
{
182
- _logger . Error ( $ "Property { propertyInfo . Name } is not an IBindingList it is { propertyInfo . PropertyType } " ) ;
156
+ OnPropertyChanged ( propertyInfo . Name ) ;
183
157
}
184
158
}
185
-
186
- if ( typeof ( INotifyPropertyChanged ) . IsAssignableFrom ( propertyInfo . PropertyType ) )
159
+ else
187
160
{
188
- var notifyPropertyChanged = propertyInfo . GetValue ( this ) as INotifyPropertyChanged ;
189
- if ( notifyPropertyChanged != null )
190
- {
191
- notifyPropertyChanged . PropertyChanged += ( _ , _ ) => { OnPropertyChanged ( propertyInfo . Name ) ; } ;
192
- }
193
- else
194
- {
195
- _logger . Error ( $ "Property { propertyInfo . Name } is not an INotifyPropertyChanged it is { propertyInfo . PropertyType } ") ;
196
- }
161
+ collection . CollectionChanged += ( _ , _ ) => { OnPropertyChanged ( propertyInfo . Name ) ; } ;
197
162
}
198
163
}
164
+ else
165
+ {
166
+ _logger . Error ( $ "Property { propertyInfo . Name } is not an INotifyCollectionChanged it is { propertyInfo . PropertyType } ") ;
167
+ }
199
168
}
200
- catch ( Exception e )
169
+
170
+ if ( typeof ( IBindingList ) . IsAssignableFrom ( propertyInfo . PropertyType ) )
201
171
{
202
- _logger . Exception ( e ) ;
172
+ //Set list changed event to trigger on property change
173
+ var collection = propertyInfo . GetValue ( this ) as IBindingList ;
174
+ if ( collection != null )
175
+ {
176
+ collection . ListChanged += ( _ , _ ) => { OnPropertyChanged ( propertyInfo . Name ) ; } ;
177
+ }
178
+ else
179
+ {
180
+ _logger . Error ( $ "Property { propertyInfo . Name } is not an IBindingList it is { propertyInfo . PropertyType } ") ;
181
+ }
182
+ }
183
+
184
+ if ( typeof ( INotifyPropertyChanged ) . IsAssignableFrom ( propertyInfo . PropertyType ) )
185
+ {
186
+ var notifyPropertyChanged = propertyInfo . GetValue ( this ) as INotifyPropertyChanged ;
187
+ if ( notifyPropertyChanged != null )
188
+ {
189
+ notifyPropertyChanged . PropertyChanged += ( _ , _ ) => { OnPropertyChanged ( propertyInfo . Name ) ; } ;
190
+ }
191
+ else
192
+ {
193
+ _logger . Error ( $ "Property { propertyInfo . Name } is not an INotifyPropertyChanged it is { propertyInfo . PropertyType } ") ;
194
+ }
203
195
}
204
196
}
205
197
0 commit comments