Prism icon indicating copy to clipboard operation
Prism copied to clipboard

[BUG] Getting exception while navigating in Release mode on iOS

Open Himanshu045 opened this issue 1 year ago • 2 comments

Description

I'm having a strange issue with .net MAUI, when I run app in release mode on iOS navigation is not working, I'm getting below exception. When i'm clicking first time on button for Navigation i'm getting below exception,

Prism.Navigation.NavigationException: An error occurred while resolving the page. This is most likely the result of invalid XAML or other type initialization exception

second time when I'm clicking same button I'm getting different exception,

Prism.Navigation.NavigationException: An unsupported event occurred while Navigating. The attempted Navigation Stack is not supported by .NET MAUI

App is working fine in debug mode on both Android and iOS, but in release mode it is only working on Android.

Steps to Reproduce

Create new blank MAUI app Add Prism.DryIoc.Maui 9.0.271-pre

  • Add two View: named ViewA, ViewB
  • Add ViewModels: named ViewModelA, ViewModelB Add a button on ViewA to Navigate to ViewB Add command on ViewModelA for navigation

private async Task DoGoToViewB() { _navigationService.NavigateAsync("ViewB") .OnNavigationError(ex => Console.WriteLine("\nDoGoToViewB-------- " + ex)); ; }

register View and ViewModels in MauiProgram

private static void RegisterNavigationDependency(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation<NavigationPage>(); containerRegistry.RegisterForNavigation<ViewA, ViewModelA>(); containerRegistry.RegisterForNavigation<ViewB, ViewModelB>(); }

set onAppStart:

.UseMauiApp<App>(). UsePrism(prism => { prism.RegisterTypes(RegisterNavigationDependency); prism.OnAppStart("ViewA");

        })
        

Platform with bug

.NET MAUI

Affected platforms

iOS

Did you find any workaround?

No response

Relevant log output

No response

Himanshu045 avatar Mar 01 '24 05:03 Himanshu045

All bug reports require a reproduction app

dansiegel avatar Mar 01 '24 13:03 dansiegel

TestNav.zip @dansiegel Please find reproduction app

Himanshu045 avatar Mar 01 '24 14:03 Himanshu045

Hi what is the status of this issue? I think I'm having the same issue on my end.

Baraiboapex avatar May 30 '24 17:05 Baraiboapex

Unable to reproduce and you do not have any exception details here. Note about your reproduction:

  1. "\ViewB" should be "/ViewB"
  2. Do not register NavigationPage. In Prism.Maui we have introduced the PrismNavigationPage. Both the NavigationPage and TabbedPage are registered for you automatically. If you require a custom NavigationPage for some reason be sure to use the PrismNavigationPage as the base type.

dansiegel avatar May 30 '24 17:05 dansiegel

Okay Thanks!

Baraiboapex avatar May 30 '24 18:05 Baraiboapex

Okay Thanks!

@dansiegel

Here's the thing though, do you know why this works in debug, but then has this issue in realease mode?

Baraiboapex avatar May 30 '24 18:05 Baraiboapex

I never was able to reproduce it and you haven't actually given me an error that you're hitting so anything I could tell you would be purely a guess. That said there should be nothing Prism specific that would cause this.

dansiegel avatar May 30 '24 19:05 dansiegel

@dansiegel (I am not the same guy, lol) This may not be relevant to this post, and I do apologize for bothering you, but the reason why my code was not working in my case in release mode was because of the fact that when I included value converters, the maui UI toolkit (xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit") along with some other custom controls from an xmlns namespace definitions. it breaks. However, when I remove all of these things from the XAML document, it runs just fine, although with missing functionality.

I also have a custom base nav page extending from the prism navigation page:

    internal class BaseNavigationPage : PrismNavigationPage
    {
#if ANDROID
        INavigationPageController NavigationPageController => this;

        public new event EventHandler BackButtonPressed;

        public BaseNavigationPage()
        {
            BarTextColor = Colors.White;
            BarBackgroundColor = Color.FromArgb("#4085c0");
            BackButtonPressed += HandleBackButtonPressed;
        }
        public BaseNavigationPage(Page page) : base(page)
        {
            BarTextColor = Colors.White;
            BarBackgroundColor = Color.FromArgb("#4085c0");
            BackButtonPressed += HandleBackButtonPressed;
        }

        private async void HandleBackButtonPressed(object sender, EventArgs args)
        {
            await MvvmHelpers.HandleNavigationPageGoBack(this);
        }
#else
        public BaseNavigationPage()
        {
            BarTextColor = Colors.White;
            BarBackgroundColor = Color.FromArgb("#4085c0");
        }

        public BaseNavigationPage(Page page)
            : base(page)
        {
            BarTextColor = Colors.White;
            BarBackgroundColor = Color.FromArgb("#4085c0");
        }
#endif

        protected override bool OnBackButtonPressed()
        {
#if ANDROID
            if (CurrentPage.SendBackButtonPressed())
                return true;

            if (NavigationPageController.StackDepth > 1)
            {
                BackButtonPressed.Invoke(this, EventArgs.Empty);
                return true;
            }

            return false;
#else
            return base.OnBackButtonPressed();
#endif
        }
    }

As for the Error:

An error occurred while resolving the page. This is most likely the result of invalid XAML or other type initialization exception

And my code for the prism startup method in the maui program file:

public static void Configure(PrismAppBuilder builder)
{
    builder.RegisterTypes(RegisterTypes).CreateWindow(navigationService =>
    {
        var builder = navigationService.CreateBuilder();

        builder.AddSegment("LoginPage");

        var nav = builder.NavigateAsync();

        nav.OnNavigationError(err =>
        {
            var message =  err;
        });

        return nav;
    });
}

As for more details, I will gladly give them to you with a test project if you wish in a zip file.

Baraiboapex avatar May 30 '24 19:05 Baraiboapex

I apologize for the poor formatting.

Baraiboapex avatar May 30 '24 19:05 Baraiboapex

@Baraiboapex I realize you're not the original reporter of the issue... however neither of you have actually provided an error that points to something actionable as an issue with Prism.

The exception:

An error occurred while resolving the page. This is most likely the result of invalid XAML or other type initialization exception

Tells us that the problem is in your code that there was an exception thrown by the constructor of the Page that was being navigated to. If you look closer you'll realize that's a NavigationException that probably has a ContainerResolutionException as it's InnerException... and if you inspect that ContainerResolutionException and call ex.GetErrors() then it should help you figure out where the error is coming from. If you aren't injecting anything then the problem would happen even if you did new YourPage() which means it's not a Prism problem.

I'm also not sure why you're inheriting from PrismNavigationPage and then trying to duplicate it's logic that is going to cause you a lot of problems... There isn't anything you have there that even suggests a need for you to have a base NavigationPage in your codebase at all. The colors can be done easily with implicit styles from your app.

dansiegel avatar May 30 '24 20:05 dansiegel

Ah! That's right! I apologize I forgot about the get errors exception! Thanks for your help! I will get back to you with this information here in a little while (probably tomorrow) and will address your other points as well. I can tell you this however about the duplicate logic for the navigation page. This was added to address the problem of the prism navigation page closing suddenly after the back button is pressed on the android platform. It was provided as a work around by another person who was commenting on the related issue's thread. Here is the original link here:

https://github.com/PrismLibrary/Prism/issues/2990#issuecomment-2027892817

Do let me know if this was corrected in a new version and if I just need to update my version, because I do think that I asked when it was going to be released.

Baraiboapex avatar May 30 '24 20:05 Baraiboapex

@dansiegel

I also just wanted to say thank you for being so prompt with your replies.

And I hope that we can eventually help the original user understand what's going on.

Baraiboapex avatar May 30 '24 20:05 Baraiboapex

Update. @dansiegel This appears to be an issue with iOS as the error that I got after wading through all of the exceptions returned from the navigation exception catcher, the innermost exception says "RootViewController cannot be null." This appears to be quite a notorious issue in .NET MAUI. Based off of my current research. Let me know if you have any suggestions on how to fix this. I will get back with you tomorrow on this. Have a good one.

Baraiboapex avatar May 30 '24 21:05 Baraiboapex

@dansiegel

Sorry for getting back to you so late. I had realized that the link I had provided you is not working if you click it. You will need to cut and paste it into your browser for some reason.

Again, let me know what your thoughts are on this and if there is a better way or a fix for this issue out now.

If this is a prism issue, and if it does help (probably not) here is where exactly the app is failing in the library itself, but again, I'm not sure if this will help you:

image

Baraiboapex avatar Jun 03 '24 14:06 Baraiboapex

@dansiegel

Do you mind also showing us your implementation that you used to try and reproduce this issue? This might be a lot to ask for, but just wondering if it is possible.

Also, it looks like the issue that we were having with the app with our instance of the Newtonsoft.json compiler not reconginzing our wildcard "%VARIABLE_NAME%" values. Now we are just running into some issues of dlls that are not getting compiled: image I will continue to keep you posted on this.

Baraiboapex avatar Jun 03 '24 16:06 Baraiboapex

Okay. For anyone who is also encountering this issue.

@dansiegel is correct this is NOT a prism issue.

For me, the root window could not be created due to 2 reasons:

1.) our newtonsoft.json interpriter was having trouble with interpriting the wildcard "%VAR_NAME%" values since for some reason, the necessary replacement values were not getting loaded. Hard-coding in these said values into the configuration file worked for me.

2.) The StatusBarBehaviors was being used on the FIRST page that gets navigated to on iOS. This library apparently still has issues on iOS so simply removing it from the first page worked for me.

Again, @dansiegel thanks for the help and I am sorry to bother you :)

Although, in your case, @Himanshu045 I am curious, were you able to get your issue resolved?

Baraiboapex avatar Jun 04 '24 15:06 Baraiboapex

Also,

@dansiegel

If you have any suggested improvements for the navigation page implementation workaround that was provided in that link I gave you to cut and paste, please let me know. We can then circle back on the relevant issue within that said link.

Baraiboapex avatar Jun 05 '24 19:06 Baraiboapex

It's called... that "workaround" isn't needed

dansiegel avatar Jun 05 '24 19:06 dansiegel

@dansiegel Okay, if I provide you a sample of my code in a separate thread on the discord since we do have a paid account, do you mind helping me correct the implementation? I'm just curious I tried removing this workaround and then the project did not work for me. But when I put it back it worked again. I will provide you with a repository with identical code. I'm sorry to bother you, I just want to know the better way of doing this.

Thanks again for the prompt reply, and thank you for all you do!

Baraiboapex avatar Jun 05 '24 20:06 Baraiboapex