UWP页面状态管理

Muh*_*eef 2 state suspend win-universal-app

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

Gra*_*eng 6

或者示例导航到page1然后我导航到page2,但是当我导航回page1时,UI元素必须已经存在与之前相同的数据,并且它们不能重新初始化或者数据不能再被绑定到编译器.

对于此问题,您可以使用UIElement.CacheMode属性Frame.CacheSize属性.CacheSizeproperty设置导航历史记录中可以为框架缓存的页面数,CacheMode属性设置一个值,该值指示在可能的情况下应将渲染内容缓存为合成位图.

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

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

    rootFrame.CacheSize = 2;
    Window.Current.Activate();
}
Run Code Online (Sandbox Code Playgroud)

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

public MainPage()
{
    this.InitializeComponent();
    this.NavigationCacheMode = NavigationCacheMode.Enabled;
}
Run Code Online (Sandbox Code Playgroud)

我还可以做什么来管理整个应用程序的状态,这样,我终止应用程序,然后当我下次启动它时,相同的状态已经存在于上次.我可以在整个申请中申请吗?

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

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();
}
Run Code Online (Sandbox Code Playgroud)

以及如何检索此信息?您可以覆盖您的OnLaunched(LaunchActivatedEventArgs e)方法,首先您需要判断您的应用程序上次,用户或使用ApplicationExecutionState枚举的系统是如何关闭的,例如:

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();
}
Run Code Online (Sandbox Code Playgroud)

但请注意,当应用关闭时,下次启动此应用时,UI元素将重新初始化,此功能只能在您上次关闭应用时导航到该页面,但该页面中的数据将迷路了.但您也可以将数据保存到本地设置,当您导航到页面时,将值设置为这些UI元素.