I created this app to answer some questions regarding MessagingCenter, but I am not able to continue the code due to a problem running the application specifically on the Android platform, if you know what may be wrong please help me. Thanks for the support.
I've tried to change some things how Result page to new Result view in messagingcenter subscribe, but i have no idea about what is happen, to me it's like not finding the message in subscribe.
App Link(GitHub)
In ResultView:
public void Registro()
{
MessagingCenter.Subscribe<ResultView>(this, "DisplayAlert", message =>
{
this.DisplayAlert("Alerta de Registro", "Mensagem DisplayAlert com registro Enviada", "Ok");
});
}
In MainPage:
ResultView ResultPage = new ResultView();
private void GoPaginaResultComRegistro(object sender, EventArgs e)
{
ResultPage.Registro();
MessagingCenter.Send<ResultView>(ResultPage, "DisplayAlert");
MessagingCenter.Unsubscribe<ResultView>(ResultPage, "DisplayAlert");
this.Navigation.PushAsync(ResultPage);
}
I wait for DisplayAlert on the other screen when sending the message, but the App simply skips the code inside subscribe.
Try this
public void Registro()
{
MessagingCenter.Subscribe<ResultView,string>(this, "DisplayAlert", async (sender,message) =>
{
await DisplayAlert("Alerta de Registro", message, "Ok");
});
}
var mensagem = "teste";
MessagingCenter.Send<ResultView,string>(ResultPage, "DisplayAlert",mensagem);
Here is some example i use in my project
in my PCL MainPage.cs
public MainPage()
{
InitializeComponent();
MessagingCenter.Send<string>("ok", "showBar");
}
in my Native android project MainActivity.cs
protected override void OnCreate(Bundle savedInstanceState)
{
MessagingCenter.Subscribe<string>(this, "showBar", (sender) =>
{
this.Window.ClearFlags(WindowManagerFlags.Fullscreen);
});
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
this.Window.AddFlags(WindowManagerFlags.Fullscreen);
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
you don't need to create new instance of your page to send as parameter.