How to navigate from tabbed view and back with .Net Maui Shell

  Kiến thức lập trình

In .Net MAUI I am trying to navigate from Login View to the Home View which is part of tabbed layout. After that I want to navigate to a completely separate Page which is not part of the tabbed layout, therefore I don’t want to see any tabs at the top or bottom.

In the AppShell I have routes and tabs added like so:

 public AppShell()
 {
     InitializeComponent();
     Routing.RegisterRoute(nameof(LoginViewModel), typeof(LoginView));
     Routing.RegisterRoute(nameof(HomeViewModel), typeof(HomeView));
     Routing.RegisterRoute(nameof(ExampleViewModel), typeof(ExampleView));

     var initialPage = new ShellContent
     {
         ContentTemplate = new DataTemplate(typeof(LoginView))
     };

     Items.Add(initialPage);

     SetupTabs();
 }

 private void SetupTabs()
 {
     var mainTab = new Tab
     {
         Title = "Main",
         Route = nameof(HomeViewModel)
     };

     mainTab.Items.Add(new ShellContent
     {
         Title = "Page1",
         ContentTemplate = new DataTemplate(typeof(HomeView))
     });

     mainTab.Items.Add(new ShellContent
     {
         Title = "Page2",
         ContentTemplate = new DataTemplate(typeof(PlaceholderView))
     });

     var secondTab = new Tab
     {
         Title = "Second",
     };

    secondTab.Items.Add(new ShellContent
     {
         Title = "Page1",
         ContentTemplate = new DataTemplate(typeof(PlaceholderView))
     });

     Items.Add(mainTab);
     Items.Add(secondTab);
 }

From LoginViewModel I am navigating like this:
await Shell.Current.GoToAsync($"//{HomeViewModel}")

which works as expected, I am navigated to HomeView and I can see tabs at the top and bottom. From this View, I want to navigate to ExampleView like so:
await Shell.Current.GoToAsync($"{ExampleViewModel}")

But I can still see the tabs at the bottom, I believe it is the correct behavior since each tab has its stack and my new View has been pushed at the top of HomeView.

However that’s not what I want to achieve, I want to navigate to a completely new page without any tabs visible and be able to navigate back if possible.

I have tried using different types of routes:

await Shell.Current.GoToAsync($"//{ExampleViewModel}")
await Shell.Current.GoToAsync($"///{ExampleViewModel}")

since I thought I had to reset the current stack to remove the tabs but doing so I am getting an exception: Global routes currently cannot be the only page on the stack I feel like I am missing something obvious but I cannot find any info about this kind of navigation. Is this even possible using default Shell navigation and if so is it possible to navigate back to a specific view in the tabbed layout which I have navigated from?

New contributor

Oreth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT