Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin forms DisplayAlert not showing when called from a function called by a delegate

When I call Greet from inside the size function DisplayAlert displays an alert as expected However when it is called from the delegate after an event it will Log to the output with the correct name (Greet Has been Called) but the DisplayAlert does not show.

   public class CustomPage : ContentPage
   {
         ...

        protected override void OnValidSizeAllocated(double width, double height)
        {
            ...

            Greet("Test");

            app.FB.GetName();

            app.FB.NameRecieved += (s,e) =>
            {
                Greet(e.Name);  
            };

        }

        public void Greet(string name)
        {
            Utils.Log("Hey " + name);
            DisplayAlert("Hey " + name, "Welcome", "OK");
        }

    }

The Code above outputs "Hey Test" and then an alert comes up saying "Hey Test, Welcome" with an OK button then it outputs "Hey Leo" (which is correct because that is the name from the Facebook account) but then no Alert shows.

like image 876
koreus737 Avatar asked Jul 08 '15 12:07

koreus737


1 Answers

Is NameReceived fired inside GetName function?

Maybe you need to put app.FB.GetName() after the "+={...};" block.

If nameReceived is correctly fired maybe Greet is not running on ui thread, try wrap your display code in

Device.BeginInvokeOnMainThread (() => {
 DisplayAlert("Hey " + name, "Welcome", "OK");
});

as described here

like image 190
Mauro Cavallin - Lemcube Avatar answered Oct 28 '22 11:10

Mauro Cavallin - Lemcube