cptalpdeniz
cptalpdeniz

Reputation: 389

Is it possible to show DisplayAlert on startup (Xamarin.Forms)

In my mobile application (xamarin forms), I'm getting data from internet so it needs internet connection. Since I have a dictionary which I initialize in App.xaml.cs and I use data from internet, I need to check for internet connection. I have seen this question where OP asks for something similar, but the answer doesn't work for me since I need to check for internet connection whenever app launches, not after MainPage is launched. For example, Clash of Clans. Whenever the app launches, the app checks for internet connection and if there's no connection, it displays a alert to user repetitively until there's a connection.

enter image description here

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using System;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace Multi
{
    public partial class App : Application
    {
        static GroupStage groupstage = new GroupStage();

        public static HtmlWeb web = new HtmlWeb();
        public static HtmlDocument doc = LoadUrlAndTestConnection();

        //The reason why I have put a method is because I wanted to try if I can use try-catch to display alert, however this didn't work.  
        public static HtmlDocument LoadUrlAndTestConnection()
        {
            bool con = true;
            while (con)
            {
                try
                {
                    doc = web.Load(someURL);
                }
                catch (Exception ex)
                {
                    var sth = new ErrorPage();
                    sth.InternetErrorDisplay();
                    con = true;
                    continue;
                }
                con = false;
            }
            return docSK;
        }

        public static Dictionary<string, Country> _countries = new Dictionary<string, Country>
        {
            ["Australia"] = new Country(1, "Australia", false, "AU", "ausFlag.png", 3, groupstage, GetScore("Australia", 3)),

        public static string[] GetScore(string name, int GroupID)
        {
            //Gets the score data from internet
        }

        public App()
        {
            InitializeComponent();

            TwitchClass.MainAsync().Wait();

            MainPage = new OpeningPage();
        }

        protected override void OnStart()
        {

        }

        protected override void OnSleep()
        {

        }

        protected override void OnResume()
        {

        }
    }
}

    //GetScore method requires internet connection as it gets the score data from internet.

and the InternetErrorDisplay method is,

public void InternetErrorDisplay() => DisplayAlert("Connection Error", "Could not detect internet connection. This application requires access to internet.", "Retry");

Is it possible to have this behaviour in xamarin forms app? How can I achieve it?

Upvotes: 2

Views: 2488

Answers (1)

Sir Rufo
Sir Rufo

Reputation: 19106

Yes, why should it not be possible?

Here is an example which uses async/await

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Threading.Tasks;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace LoadingSample
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            //MainPage = new MainPage();
        }

        protected override async void OnStart()
        {
            // shows Loading...
            MainPage = new LoadPage();

            await Task.Yield();
            // Handle when your app starts

            // Just a simulation with 10 tries to get the data
            for (int i = 0; i < 10; i++)
            {
                await Task.Delay(500);
                // await internet_service.InitializeAsync();
                await MainPage.DisplayAlert(
                    "Connection Error", 
                    "Unable to connect with the server. Check your internet connection and try again", 
                    "Try again");            
            }
            await Task.Delay(2000);
            // after loading is complete show the real page
            MainPage = new MainPage();
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

Upvotes: 3

Related Questions