How can I use NavigationPage in Maui?

Viewed 4055

I want to navigate like this in MainPage:

private async void btnNav_Clicked(object sender, EventArgs e)
{
    await Navigation.PushAsync(new Pages.Nav1Page());
}

This does not work. Then I get to know that I need to use NavigationPage to wrap MainPage, like this:

public MainWindow()
{
    Page = new NavigationPage(new MainPage());
}

But NavigationPage does not implement the IPage interface.

5 Answers

Found in the Weather21 demo app - change your app.xaml.cs like this

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
    }

    protected override Window CreateWindow(IActivationState activationState) =>
        new Window(new NavigationPage(new MainPage())) { Title = "MyApp" };
}

Try this:

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        MainPage = new NavigationPage();
        MainPage.Navigation.PushAsync(new MainPage());
    }
}

Simply

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
        MainPage = new NavigationPage(new MainPage());
    }
}

You can try this:

App.Current.MainPage = new NavigationPage(new Page1());

Page Navigation in .NET MAUI: An Overview
Source: Selva Ganapathy Kathiresan, May 27, 2022

A typical application needs a hierarchical navigation experience where the user can navigate through pages forward and backward as required.

The .NET MAUI platform provides two primary forms of page navigation to an app:

  • Shell.
  • Base navigation pages, such as FlyoutPage, TabbedPage, and NavigationPage.

In this blog, let’s see how we can integrate page navigation in your .NET MAUI application with code examples.

Related