Search code examples
xamarin.formsmvvmcross

How do I close an MvvmCross MasterDetail page?


I would like my app to show a Login page, then a MasterDetail page with a 'hamburger' menu. On the hamburger menu I want a Log Out entry that closes the Master Detail and shows the Login page again. I have all but the log out part working.

I have copied code from the MvvmCross Playgrounds sample, specifically the MixedNavFirstPage (which fakes a login procedure), MixedNavMasterDetailPage (the 'hamburger menu'), and MixedNavMasterRootContentPage.

If I try to close the MixedNavMasterDetailPage with await _navigationService.Close(this) then I get a null reference exception in MvxFormsPagePresenter.CloseMasterDetailPage()

This question How to Exit (or Navigate out of )a MasterDetail page to a simple ContentPage in Xamarin. Forms? covers what I want to do in straight Xamarin.Forms, I'm just not sure how to do the equivalent in MvvmCross.


Solution

  • In your LoginPage add the MvxContentPagePresentation attribute NoHistory = true

    Then simply navigate to the login page, and when the user has logged in, navigate to your MasterDetail page also with NoHistory = true.

    When the user logs out again, simply navigate to your LoginPage, and as the NoHistory = true the MasterDetail will be removed completely.

    [MvxContentPagePresentation(WrapInNavigationPage = false, NoHistory = true)]
    public partial class LoginPage : MvxContentPage<LoginViewModel>
    {
        public LoginPage()
        {
            InitializeComponent();
        }
    }