Forum Xamarin Cross Platform with Xamarin

Xamarin forms 1.3 Window and iOS View Controller

Hi,

I'm trying to get rid of the Window variable in iOS AppDelegate with Xamarin Forms 1.3.
However, I got lines such as below in some dependency services and would like to know how to update them without that Window variable?
Tks.

``var rootController = ((UIApplicationDelegate)(UIApplication.SharedApplication.Delegate)).Window.RootViewController;``
            var navcontroller = rootController as UINavigationController;
            if (navcontroller != null)
                rootController = navcontroller.VisibleViewController;
            rootController.PresentViewController(imagePicker, true, null);``
Tagged:

Posts

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Use UIApplication.SharedApplication.KeyWindow instead.

  • FrancoisMFrancoisM FRUniversity ✭✭

    Tks for the answer but KeyWindow is null. Am I supposed to instantiate it?

  • FrancoisMFrancoisM FRUniversity ✭✭

    I saw your answer in that post:
    https://forums.xamarin.com/discussion/33361/ios-tint-color

    And I do call base.FinishedLaunching(app, options) but keyWindow is still null.

    I even tried

    var b = base.FinishedLaunching(app, options);
                Window.MakeKeyAndVisible();
                return b;
    

    but still null :-(

  • FrancoisMFrancoisM FRUniversity ✭✭

    FinishedLaunching does instantiate keyWindow.
    However, FinishedLaunching has to be called after LoadApplication(new App()). But the MainPage setup in the App ctor is using a dependencyservice that needs keyWindow (or more precisely the rootcontroller)... Hence the issue.

  • FrancoisMFrancoisM FRUniversity ✭✭

    To conclude, if the first page needs keyWindow, it can't work. However it if doesn't but following pages do then it's ok.
    I'll deal with it but that's a constraint...

  • adamkempadamkemp USInsider, Developer Group Leader mod

    You shouldn't be trying to present a view controller before you have a window. That doesn't make sense.

  • FrancoisMFrancoisM FRUniversity ✭✭

    While my first post is about presenting, my issue later was with the google admob component whose view requires the rootcontroller...

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Still, the answer is the same: you shouldn't be doing anything that requires a root view controller before you've even created the window. It sounds like that code should move out of the App constructor and into the OnStart override.

  • FrancoisMFrancoisM FRUniversity ✭✭

    Maybe. I'll try. My project was created before xamarin forms 1.3 so I'm just starting to use these new features.

Sign In or Register to comment.