Julien Ferraro
Julien Ferraro

Reputation: 401

How to navigate in a prism master-detail Xamarin.Forms application

I' creating my first Prism/XF application. The main page is a MasterDetailPage. I've read that to change the detail page from the master page VM, I just have to call NavigationService.NavigateAsync("NewDetailPage") from the MasterPageViewModel.

When I do this, the master page changes, and becomes NewDetailPage. I want to keep the MasterDatilPage and change it's detail page.

Here is my App class

public partial class App : PrismApplication
{
    public App(IPlatformInitializer initializer = null) : base(initializer) { }

    protected override void OnInitialized()
    {
        InitializeComponent();

        NavigationService.NavigateAsync("MenuPrincipalPage/ClubHousePage");
    }

    protected override void RegisterTypes()
    {
        Container.RegisterTypeForNavigation<MenuPrincipalPage>();
        Container.RegisterTypeForNavigation<ClubHousePage>();
        Container.RegisterTypeForNavigation<AProposPage>();
    }
}

This works great. The application is launched with the correct master (MenuPrincipalPage) and the correct detail (ClubHousePage).

I MenuPrincipalPageViewModel (the master page VM), I tried a few things. First navigate directly to the detail page :

    public DelegateCommand AProposCommand { get;  private set; }

    private void APropos()
    {
        NavigationService.NavigateAsync("AProposPage", );
    }

The MasterPage is changed.

I then tried this :

    public DelegateCommand AProposCommand { get;  private set; }

    private void APropos()
    {
        NavigationService.NavigateAsync("MenuPrincipalPage/AProposPage");
    }

The MasterPage is also changed.

This code works :

    public DelegateCommand AProposCommand { get;  private set; }

    private void APropos()
    {
        NavigationService.NavigateAsync("/MenuPrincipalPage/AProposPage");
    }

But isn't there a problem here ? The full navigation stack is erased I think.

Did I do something wrong ?

Many thanks in advance for your advices, Julien

Upvotes: 0

Views: 2637

Answers (1)

user5420778
user5420778

Reputation:

It's working exactly like it should. What makes you think you are changing the MasterDetailPage? Is it because you don't have a hamburger menu? I am guess so, because how you have it setup now will result in a MasterDetailPage that has no hamburger menu and give the appearance of there not being a MasterDetailPage. You can easily test this by navigating, and the swiping from the left edge to show the Master menu.

When using a MasterDetailPage you should be wrapping your detail in a NavigationPage.

NavigationService.Navigate("MasterDetailPage/NavigationPage/Detail");

Then any navigation done within the Detail page will automatically have a back button for it's navigation stack.

Upvotes: 2

Related Questions