UWP/WinRT:如何保存然后恢复一个简单的 TextBox 以便从终止后恢复? [英] UWP/WinRT: How to save and then restore a simple TextBox for resume from termination?

查看:26
本文介绍了UWP/WinRT:如何保存然后恢复一个简单的 TextBox 以便从终止后恢复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本文档中,描述了Windows 10 UWP 应用的生命周期,它指出:

In this document describing the lifecycle of a Windows 10 UWP app, it states:

用户现在希望您的应用在他们在设备上执行多项任务时记住其状态.例如,他们希望页面滚动到相同的位置,并且所有控件都处于与以前相同的状态.通过了解启动、暂停和恢复的应用生命周期,您可以提供这种无缝行为.

Users now expect your app to remember its state as they multitask on their device. For example, they expect the page to be scrolled to the same position and all of the controls to be in the same state as before. By understanding the application lifecycle of launching, suspending, and resuming, you can provide this kind of seamless behavior.

然而,似乎没有太多关于如何实现这一目标的文档.我认为所有内容都将由应用程序开发人员手动保存,然后在恢复时使用您在应用程序暂停时隐藏的任​​何数据从头开始重新创建,所有这些都是为了营造一种错觉,即应用程序的确切内存状态从未改变.

However, there doesn't appear to be much documentation on how this is actually achieved. I gather that everything is to be manually saved by the app developer, and then recreated from scratch on resume using whatever data you stashed away when the app was suspending, all in order to create the illusion that the exact memory state of the app never changed.

我试图通过一个最小的例子来解决这个问题,一个只包含一个 TextBox 的 XAML 页面.尽管如此,即使是这种情况,我也很难理解如何实现目标.我将提供更一般的想法,但我的具体问题只是如何保存然后恢复一个简单的 TextBox 以便从终止后恢复?我在 C++/CX 中工作,但我会寻求任何帮助可以得到.

I'm trying to puzzle through this using just a minimal example, a XAML page containing nothing other than a TextBox. Even this situation, though, I'm struggling a bit to understand how to achieve the goal. I'll provide more general thoughts, but my concrete question simply is how do you save and then restore a simple TextBox for resume from termination? I'm working in C++/CX but will take any help I can get.

以下是我目前对此的看法:

Here are my thoughts on this so far:

  • 显然,至少必须保存 TextBox 的文本.
    • 这可以保存到 ApplicationData::Current->LocalSettings 中.
    • 我立即看到的一个问题是,我在上面引用的有关生命周期的文档指出,应用必须在暂停信号或面临终止的 5 秒内完成保存.一个文本框可能会保存大量数据,这会导致在面对繁忙的 IO 时可能会中断保存,特别是当我们开始扩展超出微不足道的单个文本框情况时.
    • 幸运的是,该文档指出,我们建议您为此目的使用应用程序数据 API,因为它们可以保证在应用程序进入暂停状态之前完成.有关详细信息,请参阅使用 UWP 应用程序访问应用程序数据."不幸的是,当您点击该链接时,没有任何相关内容可以提供更多详细信息,而且我在 API 中找不到任何记录此行为的内容.通过保存到 ApplicationData::Current->LocalSettings,我们是否可以避免被损坏或丢失的数据切断?
    • At minimum, obviously the text of the TextBox has to be saved.
      • This could be saved into the ApplicationData::Current->LocalSettings.
      • One issue I see immediately is that the document I cited above on lifecycles states that apps must take care of their saving within 5 seconds of the suspend signal or face termination. A Textbox could potentially hold a lot of data, causing a save to potentially be cutoff in the face of busy IO, particularly if we start scaling beyond the trivial single TextBox situation.
      • Fortunately, the document states, "We recommended that you use the application data APIs for this purpose because they are guaranteed to complete before the app enters the Suspended state. For more info, see Accessing app data with the UWP app." Unfortunately, when you follow that link, there is nothing relevant there providing any more detail, and I can't find anything documenting this behavior in the API's. By saving into ApplicationData::Current->LocalSettings are we safe from being cut off with corrupted or lost data?
      • 我们可以使用 TextBox->SelectionStart 获取光标位置,据我所知,在 API 中没有记录返回当前光标位置的这种用法.这似乎也很适合作为 int 存储到 ApplicationData::Current->LocalSettings 中.
      • 如何获取、保存和恢复 TextBox 窗口的滚动位置?
      • We can get the cursor position with TextBox->SelectionStart, which as far as I can tell, is undocumented in the API of this usage of returning the current cursor position. This seems an easy fit to also store as an int into ApplicationData::Current->LocalSettings.
      • How can we get, save, and restore the scroll position of the TextBox window?

      推荐答案

      您参考的应用生命周期文档已针对 Windows 10 进行了更新,但似乎丢失了您想知道的一些重要内容.

      The App lifecyle document you reference has been updated for Windows 10, but seems to have lost some of the important bits that you are wondering about.

      我找到了一篇旧博文,管理应用生命周期,让您的应用感觉永远活着",这似乎是您链接的灵感来源.

      I found an old blog post, Managing app lifecycle so your apps feel "always alive", that seems to be the inspiration for your link.

      在博文的末尾,有一段文字写道:

      In the blog post, there is a paragraph towards the end that reads:

      在正确的时间保存正确的数据

      在应用的整个生命周期中始终以增量方式保存重要数据.由于您的应用最多只有 5 秒的时间来运行挂起事件处理程序代码,因此您需要确保在挂起时您的重要应用数据已保存到持久存储中.

      Always save important data incrementally throughout the life of your app. Because your app has only up to five seconds to run suspending event handler code, you need to ensure that your important app data has been saved to persistent storage by the time it is suspended.

      您在编写应用时需要管理两种类型的数据:会话数据和用户数据.会话数据是与用户当前在您的应用中的体验相关的临时数据.例如,用户正在查看的当前股票、用户在电子书中阅读的当前页面或一长串项目中的滚动位置.用户数据是持久的,并且必须始终可供用户访问,无论如何.例如,用户正在输入的正在进行的文档、在应用中拍摄的照片或用户在您的游戏中的进度.

      There are two types of data for you to manage as you write your app: session data and user data. Session data is temporary data that is relevant to the user’s current experience in your app. For example, the current stock the user is viewing, the current page the user is reading in an eBook or the scroll position in a long list of items. User data is persistent and must always be accessible to the user, no matter what. For example, an in-progress document that the user is typing, a photo that was taken in the app or the user’s progress in your game.

      鉴于以上内容,我将尝试回答您的问题:

      Given the above, I'll attempt to answer your questions:

      如何保存然后恢复一个简单的 TextBox 以便从终止后恢复?

      how do you save and then restore a simple TextBox for resume from termination?

      当最终用户在 TextBox 中输入时,应用程序会在后台将内容保存到数据存储中.借用文字处理软件的工作原理,您可以自动保存文本框文档".我认为文本框内容就是上面博客文章中描述的用户数据".由于保存是在暂停之外完成的,因此无需担心时间窗口.

      As the end user is typing in the TextBox, the app saves the contents in the background to the data store. To borrow from how word processing software works, you auto-save the textbox "document". I would consider the textbox content to be what the blog post above describes as "user data". Since the save is done outside of suspension, there is no time window to worry about.

      当您的应用从终止状态恢复时,它会检查数据存储并将所有数据加载到文本框中.

      When your app resumes from termination, it checks the data store and loads any data into the textbox.

      一旦处理了最小值,接下来我们可能需要额外的东西,比如光标和窗口位置.

      Once the minimum has been taken care of, next we'll probably need extras like cursor and window position.

      我会考虑这些项目会话数据"并将在暂停期间保存它们.毕竟,当应用程序处于活动状态时,无需跟踪此信息.用户并不关心 10 分钟前开始打字时光标在哪里——他只关心暂停时光标的位置.

      I would consider these items "session data" and would save them during suspension. After all there is no need to keep track of this info while the app is active. The user doesn't care where the cursor was 10 minutes ago when he started typing - he only cares about the cursor position at the time of suspension.

      困难的东西怎么样,比如撤销历史?

      how about the hard stuff, like undo history?

      我认为撤消历史是用户数据";并且会在它发生时保存它(暂停之外).换句话说,当用户输入内容时,您的应用应该保存撤消所需的信息.

      I would consider undo history to be "user data" and would save it while it is happening (outside of suspension). In other words, as the user types in content, your app should be saving the information necessary to undo.

      这篇关于UWP/WinRT:如何保存然后恢复一个简单的 TextBox 以便从终止后恢复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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