.NET MAUI now has a WeakReferenceManager which works beautifully when sending messages from one ViewModel to another ViewModel. Is it possible to use this when sending a message from a ViewModel to a View?
Thank you to the commenters. The full answer is:
public class MyMessage {}
WeakReferenceMessenger.Default.Register<MyMessage>(this, async (m,e) =>
{
// do the work here
});
In the XAML add a command (e.g, ShowMyMessageCommand
)
In the ViewModel send the message in the RelayCommand
:
[RelayCommand]
private void ShowMyMessage()
{
WeakReferenceMessenger.Default.Send(new MyMessage());
}