1
votes

I'm developing a small application with Xamarin Forms, the app requires signed user so MainPage in App constructor is set to LoginPage.

After a user is signed on I'm changing MainPage to AppShell, this part runs smoothly but I need to redirect a user to LoginPage each time when an application was resumed.

My code looks like this:

App.xaml.cs

public App()
{
  InitializeComponent();

  DependencyService.Register<RestDataStore>();
  DependencyService.Register<CredentialsService>();

  MainPage = new LoginPage();
}

Redirect in ViewModel of LoginPage to AppShell

public void OnSubmit()
{
  ...               
  Application.Current.MainPage = new AppShell(); 
}

App.xaml.cs

protected override void OnResume()
{
  MainPage = new LoginPage();
}

when i do Page changing like this, nothing will happen.

Is there any other way to change MainPage after an application was resumed? What am I doing wrong?

2
have you verified the code in OnResume is executing? Forcing the user to login every time they task switch seems like a really bad UX design - Jason
Also why add it as a dependency service? The login shouldnt matter the platform. - zaggler
Yes I did and the event is fired correctly, but Page remains the same as before suspending. Our customer requires this behavior because of company strict security measures. - dzambo
instead of setting MainPage like that, just use await Navigation.PushAsync (new LoginPage ()); - VahidShir
I've changed code from constructor to MainPage = new NavigationPage(new LoginPage()); , the redirect to AppShell to Application.Current.MainPage.Navigation.PushAsync( new AppShell()); and OnResume to var nav = MainPage.Navigation; await nav.PushAsync(new LoginPage()); but the OnResume redirect doesn't work and now it is throwing exception about Navigating on Android, that I should use NavigationPage. - dzambo

2 Answers

0
votes

Are you using MasterDetailPage in you app?

if so:

    public static void SetDatailPage(Page page)
    {
        if (App.Current.MainPage is MasterDetailPage)
        {
            var masterPage = (MasterDetailPage)App.Current.MainPage;
            masterPage.Detail = new NavigationPage(page);
        }
    }

And

    protected override void OnResume()
    {
        // Handle when your app resumes
        SetDatailPage(new LoginPage());
    }
0
votes

This works for me:

protected override async void OnResume()
{

  await MainPage.Navigation.PushModalAsync(new NoConnectionPage());

}

The only drawback is that it will be modal.