Does WeakReferenceMessenger lack Maui MessagingCenter Compatibility? #492
-
I have numerous Xamarin Forms applications that I am migrating to Maui and they each use MessagingCenter extensively to pass messages between Views and ViewModels in order to maintain loose coupling. Unfortunately, the Maui implementation is now deprecated so I started looking for an alternative and landed on the WeakReferenceMessenger, which, at first blush, appeared to be the answer. However, one feature of MessagingCenter that does not appear to be available with IMessenger-based implementations is the ability to send and receive a user-defined object in addition to the message. This is a crucial requirement feature for my applications. For example, in Xamarin Forms, and currently with Maui I do the following: In the View's OnNavigatedTo override: In the OnNavigatingFrom override: Then in the ViewModel: This works because the MessagingCenter's Subscribe and Send methods have an overload that makes it possible to include an object reference. Upon review of WeakReferenceMessenger (and StrongReferenceMessenger, for that matter), I discovered that neither Register nor Send offers the ability to pass a user-defined object (the token appears to have a different purpose). Would the missing functionality be considered a bug or a feature request? Or is there an alternative that I should consider? Or am I misreading the implementation? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@IndianaGary you can create any type of Message class you want which can take any sort of object. For instance, something like this: public MyMessageClass {
public MyObjectClass MyObject { get; init; }
public MyMessageClass(MyObjectClass obj)
{
MyObject = obj;
}
}
messenger.Send<MyMessageClass>(new(instanceOfMyObject)); |
Beta Was this translation helpful? Give feedback.
@IndianaGary you can create any type of Message class you want which can take any sort of object.
For instance, something like this: