.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