Windows 8.1 Universal显式创建MainPage

Windows 8.1 Universal explicitly create MainPage

有什么方法可以在 Windows 8.1 通用应用程序中显式实例化 MainPage 吗? 我想使用 autofac 或类似的 IoC 容器来解决它,并在构造函数中传递要求。

您可以在您的应用程序(App.xaml.cs 文件)的 OnLaunched 方法中显式设置 Content 属性 of Frame:

 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();
    }

相反 rootFrame.Content = new MainPage(); 您可以将 Content 属性 分配给任何 Page 实例。