首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UWP页面状态管理

UWP页面状态管理
EN

Stack Overflow用户
提问于 2016-03-11 15:39:20
回答 1查看 3.2K关注 0票数 2

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-14 06:30:36

或者,例如导航到page1,然后导航到page2,但是当我导航回page1时,UI元素必须与以前的数据相同,不能重新初始化它们,或者编译器不能再次绑定数据。

对于这个问题,您可以使用UIElement.CacheMode性质Frame.CacheSize性质CacheSize属性设置导航历史记录中可为框架缓存的页数,CacheMode属性设置一个值,该值指示呈现的内容应尽可能作为复合位图缓存。

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

代码语言:javascript
复制
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...
    // Ensure the current window is active

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

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

代码语言:javascript
复制
public MainPage()
{
    this.InitializeComponent();
    this.NavigationCacheMode = NavigationCacheMode.Enabled;
}

另外,我可以做些什么来管理整个应用程序的状态,比如,我终止了应用程序,然后当我下次启动它时,与上次一样的状态已经存在了。我能把它应用于整个应用程序吗?

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

代码语言:javascript
复制
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枚举的系统关闭的,例如:

代码语言:javascript
复制
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元素。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35944277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档