Go from MasterDetailPage to ContentPage?

Apk07Apk07 Member ✭✭

When I open my app, I start with a ContentPage "Login()". Its just a lone ContentPage with a few controls on it, no navigation stack or anything.

When someone logs in, it does:
App.Current.MainPage = new Home();

"Home" is a MasterDetailPage. It has a bunch of content, a slide-out hamburger menu, and uses Navigation.PushAsync to show other pages. This is the "main" page of the app that you'd get to if you were already logged in.

My issue comes when I want to have a "Logout" button that clears everything and bumps you back to login...

  • I can't use Navigation.PushAsync because I don't want Login() to have any navigation or slide-out menus.
  • I can't use App.Current.MainPage = new Login(); because it throws a bunch of errors trying from the MasterDetailPages OnDisappearing() and other things.

How can I just dispose of the Home() MasterDetailPage and its contents and navigate back to the Login() page gracefully?

Answers

  • AnubhavRanjanAnubhavRanjan INXamarin Team Xamurai

    @Apk07 You can always remove the Navigation Bar using the below code in the code behind of the Page
    NavigationPage.SetHasNavigationBar(this, false);

    Also, can you post the code for OnDisappearing in the MasterDetailPages which is throwing errors?

    Do you think it's possible to open a free ticket here:
    https://aka.ms/xamarinsupport

    Once done, let me know and I will take the case to help you out.
    That way you can also share the sample project with which I can help you resolve the issue.

  • Apk07Apk07 Member ✭✭
    1. Open app to Login.xaml (ContentPage)
    2. Press "Login"
    3. MainPage = Home.xaml (MasterDetailPage)
    4. Press "Logout" on Home.xaml
    5. Bump pack to Login.xaml(ContentPage)

    I don't want to just hide the navigationbar, I want to remove the MasterDetailPage so I can go back to the Login page

  • AnubhavRanjanAnubhavRanjan INXamarin Team Xamurai

    @Apk07 I believe you should take a look at this:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical#manipulating-the-navigation-stack

    Instead of Login, when you click on Logout, you can use the code
    Navigation.InsertPageBefore (new LogoutPage (), this);

    This will ensure that Logout Page is inserted.

    Or else, you can clear the entire navigation stack with codes like this:

    var existingPages = Navigation.NavigationStack.ToList();
    
    foreach(var page in existingPages)
    {
        Navigation.RemovePage(page);
    }
    

    .

    .

    .

    .



    Looking for Free* Microsoft Support on Xamarin Issues, visit https://aka.ms/xamarinsupport


  • Apk07Apk07 Member ✭✭

    Thanks, I ended up having a bizarre problem that required me to copy my code into a new solution. It no longer errors after that.

  • AnubhavRanjanAnubhavRanjan INXamarin Team Xamurai

    @Apk07 So is it safe for me to assume that the issue is resolved now?

Sign In or Register to comment.