MessagingCenter.Send is not triggering MessagingCenter.Subscribe method

179 Views Asked by At

When calling the MessagingCenter.Send function from a button click, it's not triggering the Subscribe method in MainActivity.cs. Need to call the DisplayMessage() in MainActivity.cs from the Button click of MainPage.xaml.cs. What is missing in this code ?

Below is the code which I used.

MainPage.xaml.cs

 public partial class MainPage : ContentPage{

 public MainPage()
 {
     InitializeComponent();
 }

 [Obsolete]
 private void Button1_Clicked(object sender, EventArgs e)
 {
     MessagingCenter.Send<String>("user_name", "Message_text");
     // MessagingCenter.Send<MainPage>(this, "Hi");
 }
}

Subscribe in MainActivity.cs under Platforms\Android folder.

    [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
    public class MainActivity : MauiAppCompatActivity{       

    //[Obsolete]
    public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
    {
                    
        base.OnCreate(savedInstanceState, persistentState);
        Toast.MakeText(this, "OnCreate", ToastLength.Long).Show();
        MessagingCenter.Subscribe<String>(this, "Message_text", (value) =>
        {
            DisplayMessage(value);
        });

    }

    private void DisplayMessage(string value)
    {
        Toast.MakeText(this, "Message Text = " + value, ToastLength.Long).Show();
    }
}
1

There are 1 best solutions below

4
On BEST ANSWER

Please try the following code:

MainActivity.cs

    public class MainActivity : MauiAppCompatActivity
{

    public static MainActivity Instance { get; private set; }
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Instance = this;

        MessagingCenter.Subscribe<MainPage, int>(this, "PassedData", (obj, item) =>
        {
            DisplayMessage(item.ToString());
        });

    }


    private void DisplayMessage(string value)
    {
        Toast.MakeText(this, "Message Text = " + value, ToastLength.Long).Show();
    }
}

On MainPage.xaml,add a button to send message ,just as follows:

    private void OnBtnClicked(object sender, EventArgs e)
    {
        MessagingCenter.Send<MainPage, int>(this, "PassedData", 123);
    }

Note:

1.Please add your code into method

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    } 

not method

public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
    {
                    
        base.OnCreate(savedInstanceState, persistentState);

    }

2.From Publish and subscribe to messages, we know that MessagingCenter has been deprecated in .NET 7 and replaced with WeakReferenceMessenger. So, you can use Messenger instead.