How do I do I navigate back to a top-level page from a modal page in Xamarin Shell?

EasyGoingPat 261 Reputation points
2021-07-14T08:33:08.53+00:00

In Xamarin Shell, I can't figure out how to navigate back to a top-level page (one contained in the main TabBar) from a modal page that is not in the main hierarchy.

My navigation hierarchy is like the diagram below and is set up in code like this (just experimenting for now):

public partial class AppShell2 : Xamarin.Forms.Shell  
{  
	public AppShell2()  
	{  
		var mainTabBar = new TabBar() { Title = "Main Tab Bar" };  
		var homeView = new ShellContent  
		{  
			Icon = ImageSource.FromResource( _HomeIcon ),  
			ContentTemplate = new DataTemplate( typeof(HomeView) ),  
			Route = ScreenNames.Home,  
		};  
		var settingsView = new ShellContent  
		{  
			Icon = ImageSource.FromResource( _SettingsIcon ),  
			ContentTemplate = new DataTemplate( typeof(SettingsView) ),  
			Route = ScreenNames.Settings  
		};  
		mainTabBar.Items.Add( homeView );  
		mainTabBar.Items.Add( settingsView );  

		Items.Add( mainTabBar );  

		Routing.RegisterRoute( ScreenNames.ScreenA, typeof(ScreenAView) );  
		Routing.RegisterRoute( ScreenNames.ScreenB, typeof(ScreenBView) );  
	}  
}  

114544-shell-navigation-diagram.png

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-07-15T03:17:44.843+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    how to navigate back to a top-level page (one contained in the main TabBar) from a modal page that is not in the main hierarchy

    Try to store the route of the 'top-level page' before navigating to the modal page and then go back to the 'top-level page' using Shell.Current.GoToAsync(route) command. You could the Properties dictionary of App class to save the route value.

    Check the code:

       public partial class HomePage : ContentPage  
       {  
           string className;  
           public HomePage()  
           {  
               InitializeComponent();  
               className = this.GetType().Name;  
           }  
         
           private void Button_Clicked(object sender, EventArgs e)  
           {  
               Application.Current.Properties["top_route"] = className;  
               Navigation.PushModalAsync(new ModalPage_1());  
           }  
       }  
         
       public partial class ModalPage_1 : ContentPage  
       {  
           public ModalPage_1()  
           {  
               InitializeComponent();  
           }  
         
           private void Button_Clicked(object sender, EventArgs e)  
           {  
               Navigation.PushModalAsync(new ModalPage_2());  
           }  
       }  
         
       public partial class ModalPage_2 : ContentPage  
       {  
           public ModalPage_2()  
           {  
               InitializeComponent();  
           }  
         
           private void Button_Clicked(object sender, EventArgs e)  
           {  
               var top_route = Application.Current.Properties["top_route"] as string;  
               Shell.Current.GoToAsync("//" + top_route);  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful