Home > Enterprise >  'before must be a child of the NavigationPage (Parameter 'before')' in Xamarin/.
'before must be a child of the NavigationPage (Parameter 'before')' in Xamarin/.

Time:10-27

I was creating a demo app in .Net Maui & wanted to insert a page before navigation by using InsertPageBefore method of Navigation.

Referred from here

I am at MainPage & navigation to Page4 but before navigation to Page4 I want to insert Page3 to make my navigation stack looks like below

MainPage/Page3/Page4

This is the code for navigation to Page4

private void Page4ButtonClicked(object sender, EventArgs e)
{
   Navigation.InsertPageBefore(new Page3(),new Page4());
}

My App.cs code

public App()
{
    InitializeComponent();

    MainPage = new NavigationPage(new MainPage());
}

The error message I am getting is 'before must be a child of the NavigationPage (Parameter 'before')'

CodePudding user response:

Thanks to @Json & @ewerspej comments.

I was calling Navigation.InsertPageBefore(new Page3(),new Page4()); from MainPage.

which should have been called after navigating to Page4.

So if you want to add a page in the navigation stack after navigation.

Call the Navigation.InsertPageBefore(new Page3(),this); method like this.

Posting this as an answer for others who is still stuck in this issue.

  • Related