How to properly set Device.BeginInvokeOnMainThread to await DisplayAlert response?

LucasSossaiLucasSossai Member ✭✭✭
edited January 29 in Xamarin.Forms

Greetings everyone, I'm trying to display an Alert for my user but the function is not awaiting the Device.BeginInvokeOnMainThread to give the correct wannaChange, I've tryied somethings but didnt manage to get the correct answer , am I implementing the await/async wrong?

        public async Task<bool> QuerSincronizarRelogio()
        {
            bool wannaChange = false;
            Device.BeginInvokeOnMainThread(async () =>
            {
                try
                {
                    var answer = await AirSenseApp.App.Current.MainPage.DisplayAlert("Sem conexão com internet", "Gostaria de prosseguir com Acesso Local?", "Sim", "Não");
                    if(answer)
                    {
                        Console.WriteLine("\n Wanna Change True \n ");
                        wannaChange = true;
                    }
                    else
                    {
                        Console.WriteLine("\n Wanna Change False \n ");
                        wannaChange = false;
                    }
                }
                catch(Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            });
            Console.WriteLine("\n Return WannaChange {0} \n ",wannaChange);
            return wannaChange;
        }

I found a workaround but is really uggly, I think there is a cleaner way to solve this

        public async Task<bool> QuerSincronizarRelogio()
        {
            bool wannaChange = false;
            string wtf = "";
            Device.BeginInvokeOnMainThread(async () =>
            {

                var answer = await AirSenseApp.App.Current.MainPage.DisplayAlert("Sem conexão com internet", "Gostaria de prosseguir com Acesso Local?", "Sim", "Não");

                if(answer)
                {
                    Console.WriteLine("\n Wanna Change True \n ");
                    wtf = "1";
                    wannaChange = true;
                }
                else
                {
                    wtf = "1";
                    Console.WriteLine("\n Wanna Change False \n ");
                    wannaChange = false;
                }

            });
            int aux = 1;
            while(aux == 1)
            {
                if(wtf!="")
                {
                    Console.WriteLine("\n While True  {0} \n ", wtf);
                    aux = 0;
                }

                await Task.Delay(10);
            }
            Console.WriteLine("\n Return WannaChange {0} \n ", wannaChange);
            return wannaChange;
        }

Best Answers

  • ClintStLaurentClintStLaurent US ✭✭✭✭✭
    Accepted Answer

    Its DisplayAlert.
    Not DisplayAndWaitForResponseAlert.

    I don't think you can wait for it to close. You're just requesting the alert be shown. Then you're done.

    Just one of the many limitations of using it and why most of us have outgrown it and moved on to rg.plugins.popups.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    Accepted Answer

    Its DisplayAlert.
    Not DisplayAndWaitForResponseAlert.

    I don't think you can wait for it to close. You're just requesting the alert be shown. Then you're done.

    Just one of the many limitations of using it and why most of us have outgrown it and moved on to rg.plugins.popups.

  • LucasSossaiLucasSossai Member ✭✭✭

    @ClintStLaurent said:
    Its DisplayAlert.
    Not DisplayAndWaitForResponseAlert.

    I don't think you can wait for it to close. You're just requesting the alert be shown. Then you're done.

    Just one of the many limitations of using it and why most of us have outgrown it and moved on to rg.plugins.popups.

    I will try this plugin, thanks.

Sign In or Register to comment.