1
votes

I'm trying to check which page should load my app at the beginning, first of all I check a database table if I find the login information stored I want to push the once named StartPage(), as I'm working with the database the method includes an await if there isn't any data stored I want to push the LoginPage(). I have tried following this example Xamarin.Forms Async Task On Startup . My code is :

public App()
    {

        int result;
        InitializeComponent();

        ThreadHelper.Init(SynchronizationContext.Current);
        ThreadHelper.RunOnUIThread(async () => {
            MainPage = new ActivityIndicatorPage();
            result = await InitializeAppAsync();
            if (result == 0)
            {
                PushLoginPage();
            }
            else
            {
                PushStartPage();
            }
        });



    }
 public void PushStartPage()
    {
        NavigationPage nav = new NavigationPage(new StartPage());
        nav.SetValue(NavigationPage.BarBackgroundColorProperty, Color.FromHex("#D60000"));
        MainPage = nav;
    }

    public void PushLoginPage()
    {
        MainPage = new Login();
    }

    public void PushLoginPage(string email, string password)
    {
        MainPage = new Login(email, password);
    }

    private async Task<int> InitializeAppAsync()
    {
        if (ViewModel == null)
            ViewModel = new MainViewModel(this);

        return  await ViewModel.LoginViewModel.PushInitialPage();


    }

But throws the following exception and as the author of the article says, is not recommended to do it. Exception Another option tried was overriding the OnStart() method but didn't work either.

protected override async void OnStart()
    {
        Task.Run(async ()=> { await InitializeAppAsync(); });
    }

The PushInitialPage method: public async Task PushInitialPage() {

        if (_app.Properties.ContainsKey("isLogged"))
        {
            var user = await UserDataBase.GetUserDataAsync();

            var result = await Login(user.Email, user.Password);


            if (result.StatusCode != 200)
            {
                return 0;
                ///PushLoginPage();
            }
            else
            {
                return 1;
                //PushStartPage();
            }

        }
        else
        {

            return 0;
        }
    }
2
What happened with OnStart version? You shouldn't need that Task.Run just override the method with async and execute your async code on the UI Context. The Task.Run is throwing it on the background.JSteward

2 Answers

1
votes

When the OS asks your app to show a page, it must show a page. It can't say "hold on a minute or two while I talk to this remote server over an iffy network connection." It has to show a page Right Now.

So, I recommend bringing up a splash page - your company or app logo, for example. When the splash page shows, then call InitializeAppAsync, and based on the result, switch to the login or start page or nice user-friendly offline error page.

0
votes

In Xamarin.Forms we have properties called 'Application.Current.Properties'. By using this we can able to save the any data type. So once user login in to the application you can set one flag and set it is true. Then after every time when user login in to the application you can check this flag and navigate your respective page.

Sample Code :

App.cs :

public App()
{
 if (Current.Properties.ContainsKey("isLogged"))
 {
    if((bool)Application.Current.Properties["isLogged"])
    {
       // navigate to your required page.
    }
    else
    {
      // naviate to login page.
    }
 }
 else
 {
   // naviate to login page.
 }
}

At first time application open it checks the 'isLogged' property is presented or not, if not it will move to the login page. When user login into the application by using his credentials, we need to create 'isLoggin' property and set as true. Then after if user try to login it checks the condition and navigates to the respective page.

Saving Property SampleCode :

Application.Current.Properties["isLogged"] = true;

await Application.Current.SavePropertiesAsync();

write above code for after login into the application. If a user log out from the app you need to set 'isLogged' flag is false.