How can I use WeakReferenceMessenger instead of MessagingCenter in my iOS project?

Kim Strasser 661 Reputation points
2023-01-29T16:30:19.28+00:00

I get the following message when I use MessagingCenter in my .NET 7.0 iOS project:

[deprecated] class Microsoft.Maui.Controls.MessagingCenter Associates a callback on subscribers with a specific message name. CS0618: 'MessagingCenter' is obsolete: 'We recommend migrating to CommunityToolkit.MVVM.WeakReferenceMessenger: https://www.nuget.org/packages/CommunityToolkit.Mvvm'

In my iOS project:

MyViewController.cs:

using UIKit;
using SharedCode;
using Microsoft.Maui.Controls;

namespace MyProjectiOS
{
    public partial class MyViewController : UIViewController
    {
        private UIImageView LogoImage;

        public MyViewController() : base("MyViewController", null)
        {

        }

        public override void ViewDidLoad()
        {
            View = new UIView();
            View.BackgroundColor = UIColor.White;
            base.ViewDidLoad();

            UIView viewbg = new UIView(new CoreGraphics.CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height));
            View.AddSubview(viewbg);

            MessagingCenter.Subscribe<Game1>(this, "Hi", (sender) =>
            {
                G_RemoveLogoTestMyView();
            });

            LogoImage = new UIImageView(UIImage.FromBundle("Image"));
            LogoImage.ContentMode = UIViewContentMode.ScaleAspectFit;
            LogoImage.Tag = 1234;
            LogoImage.Frame = new CoreGraphics.CGRect(0, 0, 375, 375);
            viewbg.AddSubview(LogoImage);
            LogoImage.Center = viewbg.Center;
        }

        public void G_RemoveLogoTestMyView()
        {
            //remove image
            var LogoView = View.ViewWithTag(1234);
            if (null != LogoView)
                LogoView.RemoveFromSuperview();
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
        }

    }

}

In my SharedCode project, I use the following code to remove LogoImage:

Game1.cs:

#if __IOS__    
    MessagingCenter.Send<Game1>(this, "Hi");

How can I use WeakReferenceMessenger instead of MessagingCenter in my iOS project?

I only want to use WeakReferenceMessenger to remove LogoImage.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,607 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.
9,974 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 64,571 Reputation points Microsoft Vendor
    2023-01-30T02:42:22.9366667+00:00

    Hello,

    Firstly, please install CommunityToolkit.Mvvm nugget package.

    Then please create a class in your SharedCode project like following code.

    public class Game1Message : ValueChangedMessage<Game1>
    {    public Game1Message(Game1 game1) : base(game1)
        {
        }
    }
    

    Send the message like following code and delete MessagingCenter.Send<Game1>(this, "Hi"); line. Note:Based on your Game1 generic, I send the Game Object for testing.

            Game1 game1= new Game1();
            WeakReferenceMessenger.Default.Send(new Game1Message(game1));
    

    Open ViewDidLoad method, replace MessagingCenter.Subscribe with the following code.

     WeakReferenceMessenger.Default.Register<Game1Message>(this, (r, m) =>
            {
                // Handle the message here, with r being the recipient and m being the 
               // input message. Using the recipient passed as input makes it so that
                // the lambda expression doesn't capture "this", improving performance.
                G_RemoveLogoTestMyView();
            });
    

    Here is a document about Sending and receiving messages

    Best Regards,

    Leon Lu


    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