Skip to content

Commit 5a65e6c

Browse files
authored
Update README.md
1 parent 752bf07 commit 5a65e6c

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

README.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ ReadOnlyObservableCollection<MostRecentlyUsedFileItem> mruList = mruManager.Most
348348

349349
----
350350
### `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.
352353

353354
#### Example
354355
##### Aggregate events
@@ -369,32 +370,63 @@ aggregator.TryRegisterObservable(mainWindowViewModel,
369370
nameof(INotifyPropertyChanged.PropertyChanged),
370371
nameof(MainWindowViewModel.ItemCreated)
371372
});
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)});
374380
```
375381

376382
##### Listen to all aggregated event sources by event name
377383
Subscribe to the `EventAggregator` and listen to specific events of all aggregated event sources:
378384

379385
```C#
380386
// 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);
382390
```
383391

384392
##### Listen to specific aggregated event sources by event name
385393
Subscribe to the `EventAggregator` and listen to specific events of specific aggregated event sources:
386394

387395
```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);
393407
```
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));
394415

416+
// Subscribe by defining the EventArgs as generic type parameter
417+
aggregator.TryRegisterGlobalObserver<PropertyChangedEventArgs>(ShowMessage_OnPropertyChanged);
418+
```
419+
395420
##### Type declarations used in examples
396421

397422
```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
398430
class MainPageViewModel : IPage, INotifyPropertyChanged
399431
{
400432
public string Title

0 commit comments

Comments
 (0)