How do i use the PropertyChangedMessage of a property annotated with [AlsoBroadcastChange]? #231
-
Hi, I am testing the new features of the CommunityToolkit.Mvvm 8.0.0-preview3, and would like to know how to properly make use of the broadcast message generated by the [AlsoBroadcastChange] attribute. Here is an example: [ObservableObject]
[ObservableRecipient]
public partial class InputAViewModel
{
[ObservableProperty]
[AlsoBroadcastChange]
private string inputA;
}
[ObservableObject]
[ObservableRecipient]
public partial class InputBViewModel
{
[ObservableProperty]
[AlsoBroadcastChange]
private string inputB;
} Question:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
For future reference and if someone has the same question, i found two ways to accomplish the desired outcome: Using the IRecipient interface: [ObservableObject]
[ObservableRecipient]
public partial class InputBViewModel : IRecipient<PropertyChangedMessage<string>>
{
[ObservableProperty]
[AlsoBroadcastChange]
private string inputB = string.Empty;
public void Receive(PropertyChangedMessage<string> msg)
{
if (msg.Sender.GetType() == typeof(InputAViewModel))
{
if (msg.PropertyName == nameof(InputAViewModel.InputA))
InputB = msg.NewValue;
}
}
} Obs: Multiple IRecipient interfaces can be used, one for each message type to receive and each requiring a Receive method overload for its type. Like this: [ObservableObject]
[ObservableRecipient]
public partial class InputBViewModel : IRecipient<PropertyChangedMessage<string>>, IRecipient<PropertyChangedMessage<int>>
{
[ObservableProperty]
[AlsoBroadcastChange]
private string inputB = string.Empty;
public void Receive(PropertyChangedMessage<string> msg)
{
//do something with message string value
}
public void Receive(PropertyChangedMessage<int> msg)
{
//do something with message int value
}
} The second way requires writing the constructor and the OnActivate method so they wont be generated by default, and each message has to be registered manually: [ObservableObject]
[ObservableRecipient]
public partial class InputBViewModel
{
[ObservableProperty]
[AlsoBroadcastChange]
private string inputB = string.Empty;
public InputBViewModel()
{
Messenger = WeakReferenceMessenger.Default;
IsActive = true;
}
protected virtual partial void OnActivated()
{
Messenger.Register<PropertyChangedMessage<string>>(this, (obj, msg) => Receive(msg));
Messenger.Register<PropertyChangedMessage<int>>(this, (obj, msg) => ReceiveNumber(msg));
}
public void Receive(PropertyChangedMessage<string> msg)
{
//do something with message string value
}
public void ReceiveNumber(PropertyChangedMessage<int> msg)
{
//do something with message int value
}
} |
Beta Was this translation helpful? Give feedback.
-
I do not find |
Beta Was this translation helpful? Give feedback.
For future reference and if someone has the same question, i found two ways to accomplish the desired outcome:
Using the IRecipient interface:
Obs: Multiple IRecipient interfaces can be used, one for each message type to receive and each requiring a Receive method overload…