.NET MAUI: Switching the App's MainPage multiple times leads to exception

Viewed 91

The root of a .NET MAUI application is the class App : Application.

This class has a property Page? MainPage that contains the current root page.

I try to use this property for navigation. I wrote a method App.ShowPage(Page page) that sets the property MainPage to the given page.

This works like a charm! I start my application and show a login page - works.

When the user clicks login on my login page I show the main view (a TabbedPage) - works.

When the user clicks on an item on that main view a details view gets shown - works.

Now when the user clicks cancel on that detail view, I try to navigate back to the main view. As always with my method App.ShowPage(Page page). Now I get an exception:

IllegalStateException; The specified child already has a parent. You must call removeView() on the child's parent first.

So the error message tries to tell me what to do. And I do find properties like Element Parent or Element RealParent on the Page page I try to open - but they are all null. So how do I remove the page from that parent? What is the removeView() method in the .NET MAUI world?

Thanks in advance for any help!

0 Answers
Related