.Net Maui Shell.Current.Navigation.NavigationStack always has one item

154 Views Asked by At

I created a stock Maui shell app. I have the main page and three other pages. I have enabled the flyout menu on the shell page. Below is the Shell Xaml

<ShellContent
    Title="Home"
    ContentTemplate="{DataTemplate local:MainPage}"
    Route="MainPage" />

<ShellContent
    Title="Page 1"
    ContentTemplate="{DataTemplate local:Page1}"
    Route="Page1" />

<ShellContent
    Title="Page 2"
    ContentTemplate="{DataTemplate local:Page2}"
    Route="Page2" />

<ShellContent
    Title="Page 3"
    ContentTemplate="{DataTemplate local:Page3}"
    Route="Page3" />

I launch the app, the main page appears. I click the hamburger menu and navigate to page one. Then click the hamburger menu and navigate to page two. Then click the hamburger menu and navigate to page three.

On page three, I have to check how many pages are in the navigation stack as follows:

private void btnGetStack_Clicked(object sender, EventArgs e)
{
    var temp = Shell.Current.Navigation.NavigationStack.Count();
}

When I run this, there is always only 1 entry in the NavigationStack and it is always null.

I also added a button to page 3 to attempt to go back a page as seen below but it does not work either.

private async void btnGoBack_Clicked(object sender, EventArgs e)
{
    await Shell.Current.GoToAsync("..");
}

I need to be able to use the navigation stack. What am I doing wrong? I have looked at this post: .NET MAUI Shell: How can `ShellContent` page be added to the navigation stack to enable "navigate back" behavior? which seems to indicate none of this is possible with the shell navigation. Is that true?

Thanks in advance for any thoughts.

1

There are 1 best solutions below

3
On BEST ANSWER

If using //route The route hierarchy will be searched for the specified route, upwards from the current position. The matching page will replace the navigation stack.

When you navigating from Flyout, seems it use //route like below

enter image description here

I think that's the reason why the navigation stack is always one item.

For more info, please refer to Relative routes.