mchm
mchm

Reputation: 73

Cannot display an alert on app launch Xamarin Forms

I’m working on a Xamarin Forms app in which I want to perform some actions on launch. Part of the method that I want to call is responsible for checking whether some conditions are fulfilled, if not I want to display an alert (using DisplayAlert method) to inform user about the actions that he/she should take in order to allow for correct execution of apps functions. Unfortunately, on Android the alert is not being displayed, it doesn’t happen always, sometimes it gets displayed, but in most of the cases it doesn’t. After doing some research I think that the issue might be connected with threading. I added some breakpoints and I noticed that on Android the line of code responsible for displaying an alert is being executed before the page becomes visible. On iOS everything works fine (alert is being displayed when around half of the UI is shown). I have tried to execute everything from the main thread. On Android it helped partially, alert is being displayed correctly in more cases, although I still didn’t achieve 100% accuracy. Moreover, on iOS mentioned changes caused an app to get stuck on splash screen. Below there are two versions of the code that I’ve tried to use.

First the base code that I've started with:

    public partial class App : Application
    {
        MainPage mainPage;
        public static string DatabaseLocation = string.Empty;

        public App(string databaseLocation)
        {
            //All of the UI code is written in C# in MainPage initializer
            mainPage = new MainPage();
            MainPage = new NavigationPage(mainPage);
            DatabaseLocation = databaseLocation;
        }



        protected override async void OnStart()
        {
            base.OnStart();

            //in DoThings there is a code containing DisplayAlert
            await mainPage.DoThings();
        }
    }

Below is version of the code in which I've tried to put everything on main thread:

public partial class App : Application
    {
        MainPage mainPage;
        public static string DatabaseLocation = string.Empty;

        public App(string databaseLocation)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                mainPage = new MainPage();
                MainPage = new NavigationPage(mainPage);
            });
            DatabaseLocation = databaseLocation;

        }



        protected override async void OnStart()
        {
            base.OnStart();

            Device.BeginInvokeOnMainThread(async () =>
            await mainPage.DoThings());
        }
    }

I have also tried to run from main thread the line of code specifically responsible for displaying an alert but it also didn't help. DoThings is a method inside MainPage.

public async Task DoThings() {

  if (somethingMissing()){
    Device.BeginInvokeOnMainThread(async () =>
      await DisplayAlert("Error", "Fix errors", "Ok"));
  }
  else
  {
    DoStuff();
  }
}

Upvotes: 0

Views: 568

Answers (1)

Arvind Chourasiya
Arvind Chourasiya

Reputation: 17422

In MainPage you can try this code

public async Task DoThings() {

  if (somethingMissing()){
    Device.BeginInvokeOnMainThread(async () =>
      await App.Current.MainPage.DisplayAlert("Error", "Fix errors", "Ok"));
  }
  else
  {
    DoStuff();
  }
}

Upvotes: 1

Related Questions