Prism icon indicating copy to clipboard operation
Prism copied to clipboard

[XF] new 6.2 OnInitialized running prior to App main constructor, causing page navigation prior to initializing an app.xaml.cs

Open JeffDalby opened this issue 10 years ago • 15 comments

As I'm implementing this it looks like we don't need to run the bs.run() like in the old. Seems you've got it to run it's Initialize routine without having to call it somehow...I've not looked into the new code yet so I've not looked at how you pulled that off. But what I am seeing is that it's firing the OnInitialized routine prior to the App main constructor in an app.xaml.cs file.

In our app I've switched it to using an app.xaml.cs and and app.xaml file so I can have an app wide resource dictionary for styles and such. What I'm seeing happening is that the InitializeComponent() in the app constructor fires off after the bootstrapper's OnInitialize runs which navigates to a page that requires a style in the app.xaml. End result being it can't find the style since it navigated to the page prior to the the apps InitializeComponent running and it tanks.

Now I can totally hacky get around that by doing this:

public class Bootstrapper : PrismApplication
{
    private int timesrun = 0;
    protected async override void OnInitialized()
    {
       if (timesrun != 0)
         {do work}
       timesrun++}

}

and then call bs.Initialize(); after InitializeComponent(); in the app.xaml.cs file to run it a second time. So that seems to verify things are running out of order, but it's not the prettiest solution ever. :-)

I'll copy this to the issues page as well. But figured I'd post here in case someone wants to play around with the latest stuff and needs a temporary hack to get around the problem.

JeffDalby avatar Dec 15 '15 18:12 JeffDalby

Interesting. I haven't had a chance to test that scenario yet. So it seems that if you are using the App.xaml approach, the InitializeComponent has to fire before the base ctor of the PrismApplication. Since the initialization of the PrismApplication happens in the constructor before the InitializeComponent, try moving the call to InitializeComponent to the Oninitialized method before you navigate. I'm not at my computer, so I haven't tested that, but it seems like they should work. Let me know if that works, and I'll add it to the docs.

brianlagunas avatar Dec 15 '15 20:12 brianlagunas

Yeah that was the first thing I tried.

Trying to call App.InitializeComponent() from within the bootstrapper's OnInitialize won't let you compile saying that InitializeComponent() doesn't exist.

Somehow the InitializeComponent call isn't created until you build the project. There's a defect out on it where the compiler gives you a warning that it doesn't exist, but if you call it within the App constructor once you build it all works out. So you just ignore the errors. Unfortunately whatever is piecing it together doesn't work if you call it from outside the page's class.

JeffDalby avatar Dec 15 '15 20:12 JeffDalby

Is that an intellisense error or a build error? can you hit F5 and will it run? VS has a ton of issues with Xamarin.Forms and intellisense errors.

brianlagunas avatar Dec 15 '15 21:12 brianlagunas

Intellisense error when you call InitializeComponent within App(){}, full on build error when you try calling App.InitializeComponent() from OnInitialized. And I just took a moment to actually look at the error and it's because Xamarin was Xamarin and they marked InitializeComponent private.

JeffDalby avatar Dec 15 '15 22:12 JeffDalby

You don't need to prefix with "App". There is no bootstrapper anymore. You are dealing with a base application class now.

So it will be like this:

        protected override void OnInitialized()
        {
            InitializeComponent();

            NavigationService.Navigate("MyNavigationPage/ViewA/ViewB/MainPage");
        }

At least this worked for me.

brianlagunas avatar Dec 15 '15 23:12 brianlagunas

Cool. I'll try that. Out.

Sent from my Windows Phone


From: Brian Lagunasmailto:[email protected] Sent: ‎12/‎15/‎2015 4:49 PM To: PrismLibrary/Prismmailto:[email protected] Cc: Jeff Dalbymailto:[email protected] Subject: Re: [Prism] [XF] new 6.2 OnInitialized running prior to App main constructor, causing page navigation prior to initializing an app.xaml.cs (#358)

You don't need to prefix with "App". There is no bootstrapper anymore. You are dealing with a base application class now.

So it will be like this:

    protected override void OnInitialized()
    {
        InitializeComponent();

        NavigationService.Navigate("MyNavigationPage/ViewA/ViewB/MainPage");
    }

At least this worked for me.

— Reply to this email directly or view it on GitHubhttps://github.com/PrismLibrary/Prism/issues/358#issuecomment-164934833.

JeffDalby avatar Dec 16 '15 00:12 JeffDalby

Can you confirm if that worked for you? If so I will update the Prism Template pack.

brianlagunas avatar Dec 24 '15 19:12 brianlagunas

Yes it worked perfectly.

Sent from my Windows Phone


From: Brian Lagunasmailto:[email protected] Sent: ‎12/‎24/‎2015 12:38 PM To: PrismLibrary/Prismmailto:[email protected] Cc: Jeff Dalbymailto:[email protected] Subject: Re: [Prism] [XF] new 6.2 OnInitialized running prior to App main constructor, causing page navigation prior to initializing an app.xaml.cs (#358)

Can you confirm if that worked for you? If so I will update the Prism Template pack.

— Reply to this email directly or view it on GitHubhttps://github.com/PrismLibrary/Prism/issues/358#issuecomment-167152985.

JeffDalby avatar Dec 24 '15 21:12 JeffDalby

Great, thanks for the update.

brianlagunas avatar Dec 24 '15 21:12 brianlagunas

I had the same Problem and was able to solve it as You said. Right now I have the Problem, that after each save of the App.xaml file the generated App.g.cs file inherits from Xamarin.Forms.App and not from Prism.Unity.PrismApplication. Do You have any idea how to correct this?

DOliana avatar Feb 16 '16 11:02 DOliana

Make sure both the App.xaml AND App.xaml.cs are using the PrismApplication. It sounds like you haven't updated your App.xaml to use PrismApplication

brianlagunas avatar Feb 16 '16 15:02 brianlagunas

They are. This is my App.xaml:

<PrismApplication xmlns="http://xamarin.com/schemas/2014/forms"
                  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                  x:Class="MyNamespace.App.App">
    <Application.Resources>
        <ResourceDictionary>
        ...
        </ResourceDictionary>
    </Application.Resources>
</PrismApplication>

And this is my App.xaml.xs:

public partial class App : PrismApplication
    {
        protected override void OnInitialized()
        {
            InitializeComponent();
            NavigationService.Navigate<LoginPageViewModel>();
        }
}

One Thing though: Could it be because I changed to an App.xaml after Setting everything up with Prism?

DOliana avatar Feb 16 '16 16:02 DOliana

Your syntax is wrong.

<prism:PrismApplication xmlns="http://xamarin.com/schemas/2014/forms"
                        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                        xmlns:prism="clr-namespace:Prism.Unity;assembly=Prism.Unity.Forms"
                        x:Class="Test.App">

</prism:PrismApplication>

brianlagunas avatar Feb 16 '16 16:02 brianlagunas

ARGH! Thank You very much - sometimes it's the little things...

DOliana avatar Feb 16 '16 16:02 DOliana

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 31 '20 23:01 lock[bot]