PrismLibrary / Prism

Prism is a framework for building loosely coupled, maintainable, and testable XAML applications in WPF, Xamarin Forms, and Uno / Win UI Applications..

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] System.ArgumentException: Page to remove must be contained on this Navigation Page

RobFrancisAu opened this issue · comments

Description

I am getting the following error on the latest (9.0.401-pre) Prism.DryIoc.Maui.

System.ArgumentException: Page to remove must be contained on this Navigation Page

It occurs at startup.

public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();
    builder
        .UseMauiApp<App>()
        .UsePrism(prism =>
        {
            prism.RegisterTypes(cr =>
            {
                cr.RegisterForNavigation<NavigationPage>();
                cr.RegisterForNavigation<MainPage>();
            });
            prism.CreateWindow(async (container, navigation) =>
            {
                var result = await navigation.NavigateAsync($"/{nameof(NavigationPage)}/{nameof(MainPage)}");
            });
        })
        .ConfigureFonts(fonts =>
        {
            fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
        });

#if DEBUG
    builder.Logging.AddDebug();
#endif

    return builder.Build();
}

This appears to be a new issue that wasn't present in the previous pre release. The only change that has been made is
prism.OnAppStart(... -> prism.CreateWindow(... as this was a breaking change.

I have attached a sample project here
PrismNavigationPageStartup.zip

Steps to Reproduce

  1. Create new Maui .net 8 project.
  2. Add Prism.DryIoc.Maui.
  3. Use Prism and add the following code to CreateMauiApp()
.UsePrism(prism =>
{
    prism.RegisterTypes(cr =>
    {
        cr.RegisterForNavigation<NavigationPage>();
        cr.RegisterForNavigation<MainPage>();
    });
    prism.CreateWindow(async (container, navigation) =>
    {
        var result = await navigation.NavigateAsync($"/{nameof(NavigationPage)}/{nameof(MainPage)}");
    });
})

OR

  1. Run sample project.

Platform with bug

.NET MAUI

Affected platforms

iOS, I was not able test on other platforms

Did you find any workaround?

Nope. It appears that the issue is related to the NavigationPage, without this page the error does not occur.

Relevant log output

No response

You have to do this at MauiProgram.cs

.UsePrism(prism =>
{
    prism.RegisterTypes(cr =>
    {
        //cr.RegisterForNavigation<NavigationPage>();//This is not necesarry
        cr.RegisterForNavigation<MainPage>();
    });
    prism.CreateWindow(async (container, navigation) =>
    {
        var result = await navigation.NavigateAsync($"/{nameof(NavigationPage)}/{nameof(MainPage)}");
    });
})

At App.xaml.cs

    public App()
    {
        InitializeComponent();

        //MainPage = new AppShell();//This is not necesarry
    }

And voilá

image

Thanks you @carlosxjose, turns out (per your comment) the issue in my main project was the registration of the NavigationPage

.UsePrism(prism =>
{
    prism.RegisterTypes(cr =>
    {
        cr.RegisterForNavigation<NavigationPage>(); // THIS IS THE PROBLEM
    });
    prism.CreateWindow(async (container, navigation) =>
    {
        var result = await navigation.NavigateAsync($"/{nameof(NavigationPage)}/{nameof(MainPage)}");
    });
})

This has fixed the crashing but has also caused a styling issues. I am currently styling like this, which worked in Xamarin

<Style TargetType="NavigationPage">
        <Setter Property="BarBackgroundColor" Value="{DynamicResource NavigationBarColour}" />
        <Setter Property="BarTextColor" Value="{DynamicResource NavigationBarTextColour}" />
</Style>

The DynamicResource allowed me to switch between multiple themes at runtime. I understand this is a completely seperate issue, but this did work prior to 9.0.401-pre.

@RobFrancisAu Prism does not directly use the NavigationPage but rather uses a customized PrismNavigationPage. You can simply update your style to match either of the following

<Style TargetType="NavigationPage" ApplyToDerivedTypes="True">
</Style>

<Style TargetType="prism:PrismNavigationPage">
</Style>

Fantastic, thanks @dansiegel ! That did the trick.