Home > Mobile >  The name 'MessagingCenter' does not exist in the current context
The name 'MessagingCenter' does not exist in the current context

Time:01-07

In Xamarin.Forms I used MessagingCenter but now after migrating from Xamarin.Forms to Microsoft.Maui.Essentials I get this error:

The name 'MessagingCenter' does not exist in the current context

In MyViewController.cs:

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

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

And in Game1.cs:

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

Image dependencies

Is it still possible to use MessagingCenter with Microsoft.Maui?

EDIT: It works now. I added using Microsoft.Maui.Controls; and then I don't get the error anymore.

CodePudding user response:

From the docs

MessagingCenter has been deprecated in .NET 7 and replaced with WeakReferenceMessenger in the CommunityToolkit.Mvvm NuGet package. For more information, see Messenger.

CodePudding user response:

I use TargetFramework net6.0-ios. I added using Microsoft.Maui.Controls; in MyViewController.cs and in Game1.cs and then I don't get the error anymore.

  • Related