UWP页面状态管理 [英] UWP page state manage

查看:95
本文介绍了UWP页面状态管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何管理导航之间的页面状态. 例如,导航到page1,然后导航到page2,但是当我导航回page1时,UI元素必须已经与以前一样具有相同的数据,并且不能重新初始化,或者不能通过以下方式再次绑定数据:编译器. 另外,我可以做些什么来管理整个应用程序的状态,例如,我终止该应用程序,然后在下次启动时,与上次相同的状态已经存在.我可以将其应用于整个应用程序吗?或者,如果我只想将其应用在几页上,该怎么办?任何帮助将不胜感激.

I want to learn how to manage the state of a page between navigation. for 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. 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? or what if I only want to apply it on a few pages? any help would be appreciated thanks.

推荐答案

或示例导航到page1,然后导航到page2,但是当我导航回page1时,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.

对于此问题,您可以使用

For this question, you may use UIElement.CacheMode property and Frame.CacheSize property. CacheSize property sets the number of pages in the navigation history that can be cached for the frame, and CacheMode property sets a value that indicates that rendered content should be cached as a composited bitmap when possible.

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

As we know, an UWP app default using a rootFrame for displaying several pages, we just use Navigation method to change the content in the frame. You can see this in the OnLaunched(LaunchActivatedEventArgs e) method of a blank UWP app. But how to implement cache function? For example, your app has two page and one root frame. You can define CacheSize property in your OnLaunched(LaunchActivatedEventArgs e) method for example:

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

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

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

Then in your two pages's constructed functions enable CacheMode property for example:

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?

对于此问题,您将需要使用

For this question, you will need to save the page state in the OnSuspending(object sender, SuspendingEventArgs e) method using Frame.GetNavigationState method, and you can save this state into the app's local settings. For example:

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)方法,首先,您需要使用

And how to retrieve this informaton? You can override your OnLaunched(LaunchActivatedEventArgs e) method, and at first you will need to judge how is your app be closed last time, by user, or by system using ApplicationExecutionState enumeration, for example like this:

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元素.

But be aware that when an app is closed, next time you launch this app, the UI elements will be re-initialized, this function can only navigate to the page when the last time you close your app, but the data in that page will be lost. But you can also save the data to the local settings and when you navigate to the page, set the value to those UI elements.

这篇关于UWP页面状态管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆