UWP 根框架与页面框架

UWP Root frame vs Page frame

这里是典型的App.xaml.cs代码

protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
#if DEBUG
        if (System.Diagnostics.Debugger.IsAttached)
        {
            //this.DebugSettings.EnableFrameRateCounter = true;
        }
#endif
        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)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();

            rootFrame.NavigationFailed += OnNavigationFailed;
            rootFrame.Navigated += OnNavigated;

            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Load state from previously suspended application
            }

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;

            // Register a handler for BackRequested events and set the
            // visibility of the Back button
            SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;

            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
                rootFrame.CanGoBack ?
                AppViewBackButtonVisibility.Visible :
                AppViewBackButtonVisibility.Collapsed;
        }

        if (e.PrelaunchActivated == false)
        {
            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(SignInPage), e.Arguments);
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }
    }

调用rootFrame.Navigate(typeof(SignInPage), e.Arguments);时,会创建SignInPage。在SignInPage.xaml.cs中,可能有这样的代码:this.Frame.Navigate(typeof(FramePage));this.Frame. 中的 FramerootFrame 相同吗?如果是,Page class 何时何地从 App.xaml.cs?

获得根框架

是的,这是同一个 Frame 对象,因为 Page 引用了控制其内容的框架。换句话说,导航到页面的 Frame

Frame

Gets the controlling Frame for the Page content.

来源:docs.microsoft.com

此 属性 在导航时自动设置,并且首先在 Page 对象的 OnNavigatedTo 方法中可用。