DisplayAlert only works with Application.Current.MainPage

bravAxebravAxe Member
edited August 23 in Xamarin.Forms

I've just started working on Xamarin. So, i've created a page which checks if there is an internet connection available or not in Xamarin at the very starting of opening the app.If not , i show a DisplayAlert for user.

Since, it is a page

DisplayAlert("No Internet Connection","Please check your internet connection","ok");

should work. But,it is not working.I've to use

Application.Current.MainPage.DisplayAlert("No Internet Connection","Please check your internet connection","ok");

to make it working. As far as i've read, Application.Current holds the information of current app. But what kind of infomation and what is MainPage ? How DisplayAlert is found inside MainPage ? And important one: I'm already inside a Page,so why do i need to use Application.Current.MainPage to call DisplayAlert ?

I've gone through these links(but the documentation is poor):

[Can't post link as i need to be around here little while longer]

So,my question is:

  1. What is Application.Current?

  2. What is Application.Current.MainPage?

  3. Why do i need to use Application.current.MainPage to call DisplayAlert ? I'm already inside a page,so DisplayAlert should be call directly?

In the past , directly calling "DisplayAlert" worked.I never needed to use Application.Current.MainPage

Tagged:

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited August 23

    What is Application.Current? What is Application.Current.MainPage?

    Application.Current gets the current application. MainPage represents the root page of the application.

    Why do i need to use Application.current.MainPage to call DisplayAlert ? I'm already inside a page,so DisplayAlert should be call directly?

    I've created a demo about DisplayAlert for testing, it works well. Please post the related code or share a basic demo to reproduce the issue. It'll help to get a solution.

  • JohnHardmanJohnHardman GBUniversity mod

    @bravAxe said:
    Since, it is a page

    DisplayAlert("No Internet Connection","Please check your internet connection","ok");

    should work. But,it is not working.I've to use

    Application.Current.MainPage.DisplayAlert("No Internet Connection","Please check your internet connection","ok");

    You mean it is not compiling, or it is failing in some way at run time? If failing at run time, what is happening?

    @bravAxe said:
    I'm already inside a page

    Post your code.

Sign In or Register to comment.