1

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.

MATCH
  • 100
  • 10
  • this seems like a convoluted use of MessagingCenter. If you already have an instance of ResultPage and are about to navigate to it, why not just pass a parameter on the constructor, or use a public property/method? – Jason Oct 15 '19 at 16:16
  • The purpose of the app is to practice using MessagingCenter, but if you could find another simple way to accomplish the same tasks I would be grateful if you showed me how. – MATCH Oct 15 '19 at 17:08

2 Answers2

1

First in your GoPaginaResultComRegistro() method ,you should send message after PushAsync

private void GoPaginaResultComRegistro(object sender, EventArgs e)
    {
        ResultPage.Registro();
        this.Navigation.PushAsync(ResultPage);
        MessagingCenter.Send<ResultView>(ResultPage, "DisplayAlert");
        MessagingCenter.Unsubscribe<ResultView>(ResultPage, "DisplayAlert");

    }

Second in your ResultView page, call DisplayAlert in the MainThread :

 public void Registro()
    {
        MessagingCenter.Subscribe<ResultView>(this, "DisplayAlert", message =>
        {
            Device.BeginInvokeOnMainThread( async() =>
            {
                await DisplayAlert("Alerta de Registro", "Mensage DisplayAlert com registro Enviada", "Ok");
            });

        });
    }
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • I't works now, thank you, can you explain whats happen? – MATCH Oct 17 '19 at 18:00
  • 1
    @MATCH first,DisplayAlert it depends on a page that exists in the NavigationStack,so you should pushAsync the page first,second the DisplaAlert should be called in the UI thread,so you should use Device.BeginInvokeOnMainThread,if it works,could you mark and vote it up,thanks – Leo Zhu Oct 18 '19 at 01:41
  • I can't vote, i need more reputation points, but as soon as i can i'll be back here and leave my vote. Thank you again. – MATCH Oct 18 '19 at 16:35
0

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.

Guilherme Nimer
  • 555
  • 2
  • 14