WeakReferenceMessenger.Default.Register() and WeakReferenceMessenger.Default.Unregister()

dg2k 1,381 Reputation points
2023-03-14T15:45:14.73+00:00

After upgrading to net8.0 preview, I was compelled to change the legacy Xamarin-based MessagingCenter.Subscribe() and MessagingCenter.Unsubscribe() methods to WeakReferenceMessenger.Default.Register() and WeakReferenceMessenger.Default.Unregister(), respectively.

Unless I am misunderstanding the use of the new messaging methods, when I unregister and try to register again, an exception is thrown indicating an error message:

The target recipient has already subscribed to the target message.

It is as if WeakReferenceMessenger.Default.Unregister() has no effect. The same token is used on registering and unregistering. The messaging works as expected. It just doesn't like to be registered again after presumed unregistered.

Am I using WeakReferenceMessenger the wrong way, in my case, following the Xamarin way with MessagingCenter? Perhaps, the WeakReference requires a special case in the usage?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,750 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,099 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 33,461 Reputation points Microsoft Vendor
    2023-03-16T03:09:36.6733333+00:00

    Hello,

    Comparing the official documentation, the reason for your failure should be that you did not set up a clear information structure. I tested with the following code and it works.

    Messenger class:

    public class User
    {
            public Dictionary<string, string> keyValuePairs;
    }
    public class LoggedInUserChangedMessage : ValueChangedMessage<User>
    {
            public LoggedInUserChangedMessage(User user) : base(user)
            {
            }
    }
    

    Register:

     try
    {
    	WeakReferenceMessenger.Default.Register<LoggedInUserChangedMessage>(this, (r, m) =>
    	{
                    OnMessageReceived(r, m);
    	});
    }
            catch (Exception ex)
    {
        ; // ex.Message == "The target recipient has already subscribed to the target message."
    }
    public void OnMessageReceived(object sender, LoggedInUserChangedMessage payLoad)
    {
        // process the message sent via payLoad string Dictionary  
    }
    
    

    Unregister:

    WeakReferenceMessenger.Default.Unregister<LoggedInUserChangedMessage>(this);
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful