How do I navigate between pages or views in .NET MAUI in response to user inputs?

Platform: .NET MAUI| Category: Controls

.NET MAUI’s navigation commands let you push and pop pages from the navigation stack.

public partial class MainPage : ContentPage
 {
     public MainPage()
     {
         InitializeComponent();
         Title = "Main Page";
         var navigateButton = new Button
         {
             Text = "Navigate to Second Page",
             Command = new Command(() =>
             {
                 // Use Navigation to push the SecondPage onto the navigation stack
                 Navigation.PushAsync(new SecondPage());
             })
         };

         Content = new StackLayout
         {
             Children = { navigateButton },
             VerticalOptions = LayoutOptions.CenterAndExpand,
             HorizontalOptions = LayoutOptions.CenterAndExpand
         };
	}
}
public class SecondPage : ContentPage
{
    public SecondPage()
    {
        Title = "Second Page";
        var backButton = new Button
        {
            Text = "Go Back",
            Command = new Command(() =>
            {
                // Use Navigation to pop the current page (SecondPage) from the navigation stack
                Navigation.PopAsync();
            })
        };

        Content = new StackLayout
        {
            Children = { backButton },
            VerticalOptions = LayoutOptions.CenterAndExpand,
            HorizontalOptions = LayoutOptions.CenterAndExpand
        };
    }
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.