Sending messages between view models via different channels
WeakReferenceMessenger
allows you to organize communication not only between the view and view model but also between different view models. This helps you create communication between parent/child view models or even between view models in different modules. Additionally, WeakReferenceMessenger
allows you to create different messaging channels to handle the same message type differently based on the channel.
Getting ready
Start with a new project and add the CommunityToolkit.MVVM
NuGet package.
The code for this recipe is available at https://github.com/PacktPublishing/.NET-MAUI-Cookbook/tree/main/Chapter02/c2-ViewModelCommunication.
How to do it…
Let’s create a simple app with three view models: AlertGeneratorViewModel
, Security
MonitorViewModel
, and PerformanceMonitorViewModel
. AlertGenerator
ViewModel
will generate alerts of two types: security and performance. Security
MonitorViewModel
and...