gpt4 book ai didi

textbox - UWP/WinRT : How to save and then restore a simple TextBox for resume from termination?

转载 作者:行者123 更新时间:2023-12-01 05:02:11 28 4
gpt4 key购买 nike

this document在描述 Windows 10 UWP 应用程序的生命周期时,它指出:

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.



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

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

到目前为止,这是我对此的看法:
  • 至少,显然必须保存 TextBox 的文本。
  • 这可以保存到 ApplicationData::Current->LocalSettings .
  • 我立即看到的一个问题是,我在上面引用的关于生命周期的文档指出,应用程序必须在挂起信号或面临终止的 5 秒内处理好它们的保存。一个 Textbox 可能会保存大量数据,从而导致在面对繁忙的 IO 时可能会中断保存,特别是当我们开始扩展超出微不足道的单个 TextBox 情况时。
  • 幸运的是,该文档指出,“我们建议您为此目的使用应用程序数据 API,因为它们可以保证在应用程序进入暂停状态之前完成。有关详细信息,请参阅使用 UWP 应用程序访问应用程序数据。”不幸的是,当您点击该链接时,没有任何相关内容可以提供更多详细信息,而且我在 API 中找不到任何记录此行为的内容。通过保存到 ApplicationData::Current->LocalSettings我们是否可以避免被损坏或丢失的数据切断?
  • 一旦处理了最小值,接下来我们可能需要额外的东西,比如光标和窗口位置。
  • 我们可以通过 TextBox->SelectionStart 获取光标位置,据我所知,在返回当前光标位置的这种用法的 API 中没有记录。这似乎很适合也存储为 int进入 ApplicationData::Current->LocalSettings .
  • 我们如何获取、保存和恢复 TextBox 窗口的滚动位置?
  • 既然我们有了额外的东西,那么困难的东西怎么样,比如撤销历史?我假设这是不可能的,因为 my question on Stackoverflow关于如何访问 TextBox 的撤消工具还没有答案。尽管如此,如果他们切换到另一个应用程序,这似乎确实是一个糟糕的用户体验,然后回想该应用程序从未关闭,因为我们实现了从终止后的美丽无缝恢复,并且他们的撤消历史已被清除。
  • 是否还需要为 TextBox 保存其他内容以创建理想的用户体验?我错过了什么还是有更简单的方法来做这一切?像 Microsoft 的 Edge 浏览器这样的复杂情况如何处理有几十个选项卡、表单输入、滚动位置等都需要在 5 秒内保存的复杂情况?
  • 最佳答案

    App lifecyle您引用的文档已针对 Windows 10 进行了更新,但似乎丢失了您想知道的一些重要内容。
    我找到了一篇旧博文,Managing app lifecycle so your apps feel "always alive" ,这似乎是您链接的灵感来源。
    在博客文章中,末尾有一段内容是:

    Save the right data at the right time

    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.


    鉴于上述情况,我将尝试回答您的问题:

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


    当最终用户在 TextBox 中输入时,应用程序将在后台将内容保存到数据存储中。借用文字处理软件的工作原理,您可以自动保存文本框“文档”。我认为文本框内容是上面博客文章中描述的“用户数据”。由于保存是在暂停之外完成的,因此无需担心时间窗口。
    当您的应用程序从终止状态恢复时,它会检查数据存储并将所有数据加载到文本框中。

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


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

    how about the hard stuff, like undo history?


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

    关于textbox - UWP/WinRT : How to save and then restore a simple TextBox for resume from termination?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31977234/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com