John_Kub
John_Kub

Reputation: 133

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

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.

Upvotes: 0

Views: 828

Answers (2)

John_Kub
John_Kub

Reputation: 133

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

https://learn.microsoft.com/en-us/dotnet/api/microsoft.maui.controls.messagingcenter?view=net-maui-7.0

Upvotes: 0

Jason
Jason

Reputation: 89082

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.

Upvotes: 3

Related Questions