@@ -348,7 +348,8 @@ ReadOnlyObservableCollection<MostRecentlyUsedFileItem> mruList = mruManager.Most
348
348
349
349
----
350
350
### ` EventAggregator `
351
- Dynamic implementation of the EventAggregator design pattern. Listen to events broadcasted by a specific type or by a specific event.
351
+ Dynamic implementation of the EventAggregator design pattern. Listen to events broadcasted by a specific type or by a specific event or by matching event handler signature.
352
+ Allows to listen to an event without the need to reference the source instance.
352
353
353
354
#### Example
354
355
##### Aggregate events
@@ -369,32 +370,63 @@ aggregator.TryRegisterObservable(mainWindowViewModel,
369
370
nameof (INotifyPropertyChanged .PropertyChanged ),
370
371
nameof (MainWindowViewModel .ItemCreated )
371
372
});
372
- aggregator .TryRegisterObservable (mainPageViewModel , new [] {nameof (INotifyPropertyChanged .PropertyChanged )});
373
- aggregator .TryRegisterObservable (settingsPageViewModel , new [] {nameof (INotifyPropertyChanged .PropertyChanged )});
373
+
374
+ aggregator .TryRegisterObservable (
375
+ mainPageViewModel ,
376
+ new [] {nameof (INotifyPropertyChanged .PropertyChanged )});
377
+ aggregator .TryRegisterObservable (
378
+ settingsPageViewModel ,
379
+ new [] {nameof (INotifyPropertyChanged .PropertyChanged )});
374
380
```
375
381
376
382
##### Listen to all aggregated event sources by event name
377
383
Subscribe to the ` EventAggregator ` and listen to specific events of all aggregated event sources:
378
384
379
385
``` C#
380
386
// Listen to everything that publishes the 'INotifyPropertyChanged.PropertyChanged' event
381
- aggregator .TryRegisterObserver <PropertyChangedEventHandler >(nameof (INotifyPropertyChanged .PropertyChanged ), (s , args ) => MessageBox .Show ($" 'PropertyChanged event'. Sender={sender .GetType ().Name }; Value={args .PropertyName }" ));
387
+ aggregator .TryRegisterObserver <PropertyChangedEventHandler >(
388
+ nameof (INotifyPropertyChanged .PropertyChanged ),
389
+ ShowMessage_OnPropertyChanged );
382
390
```
383
391
384
392
##### Listen to specific aggregated event sources by event name
385
393
Subscribe to the ` EventAggregator ` and listen to specific events of specific aggregated event sources:
386
394
387
395
``` C#
388
- // Only listen to the 'INotifyPropertyChanged.PropertyChanged' event of the 'mainWindowViewModel' instance
389
- aggregator .TryRegisterObserver <PropertyChangedEventHandler >(nameof (INotifyPropertyChanged .PropertyChanged ), mainWindowViewModel .GetType (), (s , args ) => MessageBox .Show ($" 'PropertyChanged event'. Sender={sender .GetType ().Name }; Value={args .PropertyName }" ));
390
-
391
- // Only listen to the 'INotifyPropertyChanged.PropertyChanged' event of all instances that implemnt 'IPage'
392
- aggregator .TryRegisterObserver <PropertyChangedEventHandler >(nameof (INotifyPropertyChanged .PropertyChanged ), typeof (IPage ), (s , args ) => MessageBox .Show ($" 'PropertyChanged event'. Sender={sender .GetType ().Name }; Value={args .PropertyName }" ));
396
+ // Only listen to the 'INotifyPropertyChanged.PropertyChanged' event raised by any instance of type 'MainWindowViewModel'
397
+ aggregator .TryRegisterObserver <PropertyChangedEventHandler >(
398
+ nameof (INotifyPropertyChanged .PropertyChanged ),
399
+ mainWindowViewModel .GetType (),
400
+ ShowMessage_OnPropertyChanged );
401
+
402
+ // Only listen to the 'INotifyPropertyChanged.PropertyChanged' event of all instances that implement 'IPage'
403
+ aggregator .TryRegisterObserver <PropertyChangedEventHandler >(
404
+ nameof (INotifyPropertyChanged .PropertyChanged ),
405
+ typeof (IPage ),
406
+ ShowMessage_OnPropertyChanged );
393
407
```
408
+ ##### Listen to all events that match the signature of the event handler or that use a matching ` EventArgs ` type
409
+ Subscribe to the ` EventAggregator ` and listen to specific events of specific aggregated event sources:
410
+
411
+ ``` C#
412
+
413
+ // Subscribe by defining the event delegate explicitly
414
+ aggregator .TryRegisterGlobalObserver (new PropertyChangedEventHandler (ShowMessage_OnPropertyChanged ));
394
415
416
+ // Subscribe by defining the EventArgs as generic type parameter
417
+ aggregator .TryRegisterGlobalObserver <PropertyChangedEventArgs >(ShowMessage_OnPropertyChanged );
418
+ ```
419
+
395
420
##### Type declarations used in examples
396
421
397
422
``` C#
423
+ // Event callback
424
+ private void ShowMessage_OnPropertyChanged (object sender , PropertyChangedEventArgs e )
425
+ {
426
+ MessageBox .Show ($" Handling 'PropertyChanged event'. Sender={sender .GetType ().Name }; Value={e .PropertyName }" );
427
+ }
428
+
429
+ // Event sources
398
430
class MainPageViewModel : IPage , INotifyPropertyChanged
399
431
{
400
432
public string Title
0 commit comments