Search code examples
c#inversion-of-controluniversal

Windows 8.1 Universal explicitly create MainPage


Is there any way to explicitly instance MainPage in Windows 8.1 universal app? I want to resolve it using autofac or similar IoC container and pass requirements in constructor.


Solution

  • You can explicitly set Content property of Frame in OnLaunched method of your application(App.xaml.cs file):

     protected async override void OnLaunched(LaunchActivatedEventArgs e)
        {
    
    
            #if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
            #endif
    
            Frame rootFrame = Window.Current.Content as Frame;
    
            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                rootFrame = new Frame();
    
                rootFrame.CacheSize = 1;
    
                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    // TODO: Load state from previously suspended application
                }
    
                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }
    
            if (rootFrame.Content == null)
            {
                #if WINDOWS_PHONE_APP
                // Removes the turnstile navigation for startup.
                if (rootFrame.ContentTransitions != null)
                {
                    this.transitions = new TransitionCollection();
                    foreach (var c in rootFrame.ContentTransitions)
                    {
                        this.transitions.Add(c);
                    }
                }
    
                rootFrame.ContentTransitions = null;
                rootFrame.Navigated += this.RootFrame_FirstNavigated;
                #endif
    
                rootFrame.Content = new MainPage();
            }
    
            // Ensure the current window is active
            Window.Current.Activate();
        }
    

    Instead rootFrame.Content = new MainPage(); you can assign Content property with any Page instance.