UWP 页面状态管理

UWP page state manage

我想了解如何管理导航之间的页面状态。 例如,导航到第 1 页,然后导航到第 2 页,但是当我导航回第 1 页时,UI 元素必须已经存在并且具有与以前相同的数据,并且不得重新初始化它们或不得重新初始化数据被编译器再次绑定。 另外我可以做些什么来管理整个应用程序的状态,这样我就终止了应用程序,然后当我下次启动它时,与上次相同的状态已经存在。我可以将它应用于整个应用程序吗?或者如果我只想在几页上应用它怎么办?任何帮助将不胜感激。

or example a navigate onto page1 and then i navigate to page2, but when i navigate back to page1, the UI elements must already be there with the same data as before and they must not be re-initialized or data must not be binded again by the compiler.

这道题可以用UIElement.CacheMode property and Frame.CacheSize propertyCacheSize 属性 设置导航历史中可以为框架缓存的页面数,CacheMode 属性 设置一个值,指示呈现的内容应缓存为尽可能合成位图。

众所周知,UWP 应用程序默认使用 rootFrame 来显示多个页面,我们只需使用 Navigation 方法来更改框架中的内容。您可以在空白 UWP 应用的 OnLaunched(LaunchActivatedEventArgs e) 方法中看到这一点。但是如何实现缓存功能呢?例如,您的应用有两个页面和一个根框架。您可以在 OnLaunched(LaunchActivatedEventArgs e) 方法中定义 CacheSize 属性 例如:

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...
    // Ensure the current window is active

    rootFrame.CacheSize = 2;
    Window.Current.Activate();
}

然后在你的两个页面的构造函数中启用CacheMode 属性 例如:

public MainPage()
{
    this.InitializeComponent();
    this.NavigationCacheMode = NavigationCacheMode.Enabled;
}

Also what I can do to manage state of whole application such that, I terminate the app and then when i launch it next time, the same state is already there as last time. can i apply it on whole application?

对于这个问题,您需要使用Frame.GetNavigationState methodOnSuspending(object sender, SuspendingEventArgs e)方法中保存页面状态,您可以将此状态保存到应用程序的本地设置中。例如:

private void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();
    Frame rootFrame = Window.Current.Content as Frame;
    string navstate = rootFrame.GetNavigationState();
    var localSettings = ApplicationData.Current.LocalSettings;
    localSettings.Values["nav"] = navstate;
    deferral.Complete();
}

以及如何检索这些信息?你可以覆盖你的 OnLaunched(LaunchActivatedEventArgs e) 方法,首先你需要判断你的应用程序上次是如何关闭的,是用户关闭的,还是系统使用 ApplicationExecutionState enumeration 关闭的,例如这样:

protected 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)
    {
        // Create a Frame to act as the navigation context and navigate to the first page
        rootFrame = new Frame();

        rootFrame.NavigationFailed += OnNavigationFailed;

        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)
    {
        // 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(MainPage), e.Arguments);
        if (e.PreviousExecutionState == ApplicationExecutionState.Terminated || 
            e.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)
        {
            object value;
            var localSettings = ApplicationData.Current.LocalSettings;
            if (localSettings.Values.TryGetValue("nav", out value))
            {
                rootFrame.SetNavigationState(value as string);
            }
            else
            {
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
        }
        else
        {
            rootFrame.Navigate(typeof(MainPage), e.Arguments);
        }
    }
    // Ensure the current window is active
    rootFrame.CacheSize = 2;
    Window.Current.Activate();
}

但请注意,当一个应用程序关闭后,下次您启动该应用程序时,UI元素将被重新初始化,该功能只能导航到您上次关闭时的页面应用程序,但该页面中的数据将丢失。但您也可以将数据保存到本地设置,当您导航到页面时,将值设置为那些 UI 元素。