Skip to content

Commit 4ae70cb

Browse files
committed
Updated ObservableRecipient docs
1 parent dad2972 commit 4ae70cb

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

docs/mvvm/ObservableRecipient.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The [`ObservableRecipient`](https://docs.microsoft.com/dotnet/api/microsoft.tool
1515

1616
The `ObservableRecipient` type is meant to be used as a base for viewmodels that also use the `IMessenger` features, as it provides built-in support for it. In particular:
1717

18-
- It has both a parameterless constructor and one that takes an `IMessenger` instance, to be used with dependency injection. It also exposes a `Messenger` property that can be used to send and receive messages in the viewmodel. If the parameterless constructor is used, the `Messenger.Default` instance will be assigned to the `Messenger` property.
18+
- It has both a parameterless constructor and one that takes an `IMessenger` instance, to be used with dependency injection. It also exposes a `Messenger` property that can be used to send and receive messages in the viewmodel. If the parameterless constructor is used, the `WeakReferenceMessenger.Default` instance will be assigned to the `Messenger` property.
1919
- It exposes an `IsActive` property to activate/deactivate the viewmodel. In this context, to "activate" means that a given viewmodel is marked as being in use, such that eg. it will start listening for registered messages, perform other setup operations, etc. There are two related methods, `OnActivated` and `OnDeactivated`, that are invoked when the property changes value. By default, `OnDeactivated` automatically unregisters the current instance from all registered messages. For best results and to avoid memory leaks, it's recommended to use `OnActivated` to register to messages, and to use `OnDeactivated` to do cleanup operations. This pattern allows a viewmodel to be enabled/disabled multiple times, while being safe to collect without the risk of memory leaks every time it's deactivated. By default, `OnActived` will automatically register all the message handlers defined through the `IRecipient<TMessage>` interface.
2020
- It exposes a `Broadcast<T>(T, T, string)` method which sends a `PropertyChangedMessage<T>` message through the `IMessenger` instance available from the `Messenger` property. This can be used to easily broadcast changes in the properties of a viewmodel without having to manually retrieve a `Messenger` instance to use. This method is used by the overload of the various `SetProperty` methods, which have an additional `bool broadcast` property to indicate whether or not to also send a message.
2121

@@ -31,18 +31,18 @@ public class MyViewModel : ObservableRecipient, IRecipient<LoggedInUserRequestMe
3131
}
3232
```
3333

34-
In the above, `OnActivated` automatically registers the instance as a recipient for `LoggedInUserRequestMessage` messages, using that method as the action to invoke. Using the `IRecipient<TMessage>` interface is not mandatory, and the registration can also be done manually (even using just an inline lambda expression):
34+
In the example above, `OnActivated` automatically registers the instance as a recipient for `LoggedInUserRequestMessage` messages, using that method as the action to invoke. Using the `IRecipient<TMessage>` interface is not mandatory, and the registration can also be done manually (even using just an inline lambda expression):
3535

3636
```csharp
3737
public class MyViewModel : ObservableRecipient
3838
{
3939
protected override void OnActivated()
4040
{
4141
// Using a method group...
42-
Messenger.Register<LoggedInUserRequestMessage>(this, Receive);
42+
Messenger.Register<MyViewModel, LoggedInUserRequestMessage>(this, (r, m) => r.Receive(m));
4343

4444
// ...or a lambda expression
45-
Messenger.Register<LoggedInUserRequestMessage>(this, m =>
45+
Messenger.Register<MyViewModel, LoggedInUserRequestMessage>(this, (r, m) =>
4646
{
4747
// Handle the message here
4848
});

0 commit comments

Comments
 (0)