I'm doing a migration to .NET MAUI from Xamarin Forms. Here I am converting the MessagingCenter of Xamarin Forms to WeakReferenceManager of .NET MAUI. This is my Following code of Xamarin Forms:
MessagingCenter.Subscribe<object, object>(this, Messages.Messenger.Messages.xyz, this.XYZ);
This code is working fine for me. But when I am converting this to WeakReferenceManager as you can see the following:
WeakReferenceMessenger.Default.Register<object, object>(this, Messages.Messenger.Messages.xyz, this.XYZ);
I get the following error:
Error CS0311 The type 'object' cannot be used as type parameter 'TToken' in the generic type or method 'IMessengerExtensions.Register<TMessage, TToken>(IMessenger, object, TToken, MessageHandler<object, TMessage>)'. There is no implicit reference conversion from 'object' to 'System.IEquatable'.
I have no clue how to fix this error. Any suggestions?
You can refer to the official document: Sending and receiving messages.
Please refer to the following code:
Note:
You can also replace the passed
stringto Object item you want to pass.Here is a usage example:
1.define a message (The
TestVisibleModelis the type of the passed object)2.Register a message:
3.Send message: