Search code examples
iosxamarinmvvmcross

Subclass UIWindow in MvvmCross 6


Before updating to MvvmCross 6 we used to subclass UIWindow on iOS and set it as the keywindow in AppDelagate to detect all events in apps, the way we used to do with Setup has now been changed in MvvmCross 6.0.

https://www.mvvmcross.com/mvvmcross-6.0.0-release/

App Start

"The way apps start with MvvmCross has now become much cleaner. MvxAppStart is now called automatically by the framework uniformly. This means you can safely delete your initialization code on platforms like iOS (the framework now will also create the key window for you)."

Can't see a way of setting your own keywindow now and passing it MvvmCross, any suggestions ?

thanks

Neil


Solution

  • As you stated, MvvmCross 6 now takes care of assigning the KeyWindow for you. In fact when reading the source code of MvvmCross 6+, You can see this in the FinishedLanchingWithOptions:

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            //window is being assigned here by MvvmCross
            if (Window == null)
                Window = new UIWindow(UIScreen.MainScreen.Bounds);
    
            MvxIosSetupSingleton.EnsureSingletonAvailable(this, Window).EnsureInitialized();
    
            RunAppStart(launchOptions);
    
            FireLifetimeChanged(MvxLifetimeEvent.Launching);
            return true;
        }
    

    However, they don't provide a hook for you to do so. In order to fix that, I would suggest overriding and do something like this:

    [Register(nameof(AppDelegate))]
    public class AppDelegate : MvxApplicationDelegate<Setup, Core.App>
    {
        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            if (Window == null)
                Window = new CustomWindow(UIScreen.MainScreen.Bounds);
    
            base.FinishedLaunching(application, launchOptions);
        }
    
    }
    

    Hope this helps