From 6a8f0aaa4c9fe80c7b7a5f40f7831a3d142c2fc8 Mon Sep 17 00:00:00 2001 From: Iridium IO Date: Sun, 6 Jul 2025 17:34:56 +1000 Subject: [PATCH 1/2] Update ObservableProperty.md Updated signature of generated `Broadcast` method sample code --- docs/mvvm/generators/ObservableProperty.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/mvvm/generators/ObservableProperty.md b/docs/mvvm/generators/ObservableProperty.md index 2125ca852..fa1376406 100644 --- a/docs/mvvm/generators/ObservableProperty.md +++ b/docs/mvvm/generators/ObservableProperty.md @@ -224,7 +224,7 @@ public string? Name if (SetProperty(ref name, value)) { - Broadcast(oldValue, value); + Broadcast(oldValue, value, "Name"); } } } From 116c3631375d33f383e08ee6e1d2bbb90d9d58c1 Mon Sep 17 00:00:00 2001 From: Iridium IO Date: Sun, 6 Jul 2025 17:56:17 +1000 Subject: [PATCH 2/2] Update ObservableProperty.md Added example minimal block --- docs/mvvm/generators/ObservableProperty.md | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/mvvm/generators/ObservableProperty.md b/docs/mvvm/generators/ObservableProperty.md index fa1376406..fd139c219 100644 --- a/docs/mvvm/generators/ObservableProperty.md +++ b/docs/mvvm/generators/ObservableProperty.md @@ -232,6 +232,30 @@ public string? Name That generated `Broadcast` call will then send a new [`PropertyChangedMessage`](/dotnet/api/microsoft.toolkit.mvvm.Messaging.Messages.PropertyChangedMessage-1) using the `IMessenger` instance in use in the current viewmodel, to all registered subscribers. +Example implementation using the default `WeakReferenceMessenger` implementation in `ObservableRecipient`: + +```csharp + +//This class sends a PropertyChanged message when the "Name" property changes +public partial class SenderViewModel : ObservableRecipient +{ + [ObservableObject] + [NotifyPropertyChangedRecipients] + private string? _name; +} + +//This class received a PropertyChanged message +public class ReceiverViewModel : ObservableRecipient, IRecipient> +{ + public ReceiverViewModel() => IsActive = true; + + public void Receive(PropertyChangedMessage message) => Trace.WriteLine($"Name Changed to: {message.NewValue}"); +} + + +``` + + ## Adding custom attributes In some cases, it might be useful to also have some custom attributes over the generated properties. To achieve that, you can simply use the `[property: ]` target in attribute lists over annotated fields, and the MVVM Toolkit will automatically forward those attributes to the generated properties.