1
votes

Dears,

I am using DisplayAlert in my project for displaying no internet connection alert. Unfortunately, it is not working.

It working fine in another part. I don't know the reason why it behaves like this.

My code:

            if (Utility.IsInternet())
            {
               UserTweetsList();
            }
            else
             {
                Debug.WriteLine("Enter no internet block");
               DisplayAlert("Alert", "No internet connection", "Ok");
             }

Code enters the else block and print the "Enter no internet block" in the output, but not displaying the alert message.

What are the possible reasons for this?

Thanks in advance....

3
Where is this code? If I remember correctly display alert needs to be on a view. - Lotok
I added this in a content page - Sreejith Sree
Did you place a break point to ensure the loop lamp into the else block? - Jason

3 Answers

5
votes

Please try the below code:

Device.BeginInvokeOnMainThread(async () => 
{ 
    await DisplayAlert("Alert", "No internet connection", "Ok"); 
});
4
votes

One possible issue is that you are calling DisplayAlert from outside of a page or view. In one of my apps I have a class separate from my view and to get around this issue and call DisplayAlert I use :

App.Current.MainPage.DisplayAlert(...);
0
votes

I had a tough time with this today and none of the fixes seemed to be working for me. I worked through it though and this is what worked for me. In the code behind class I created a public variable:

public bool deleteAccountAnswer = false;

Called the deleteAccountPopup method and placed a call back in the on completed event

deleteAccountPopup().OnCompleted(() => removeAccount());

The delete account pop up code:

public System.Runtime.CompilerServices.TaskAwaiter deleteAccountPopup()
    {
        return Device.InvokeOnMainThreadAsync(async ()=>
        {
            deleteAccountAnswer = await this.DisplayAlert("Account Deletion", "You have sure you want to delete the selected account?", "Yes", "No");
        }).GetAwaiter();
    }

... The Call Back

public void removeAccount()
    {
        if (deleteAccountAnswer) // was yes not no
        {