.NET MAUI › Dependency Injection & Services

How do you implement a messaging/event aggregator to decouple view models?

Advanced

Answer

Use CommunityToolkit.Mvvm's WeakReferenceMessenger to publish and subscribe to messages without direct references, avoiding memory leaks from strong event subscriptions. One view model sends a message; others register handlers. Unregister when appropriate, though weak references reduce leak risk.
WeakReferenceMessenger.Default.Send(new ItemAddedMessage(item));

WeakReferenceMessenger.Default.Register<ItemAddedMessage>(this,
    (r, m) => Items.Add(m.Value));
Real-world example Adding an item on an edit page notifies the list page to refresh via a message, with neither page holding a reference to the other.

Common follow-ups: Why weak references? | When should you unregister?