gpt4 book ai didi

javascript - Windows UWP应用程序生命周期: how to prevent reload in JS/HTML/CSS app

转载 作者:行者123 更新时间:2023-11-27 22:32:25 25 4
gpt4 key购买 nike

我有一个用 HTML/JS/CSS 构建的简单 UWP 应用程序。它本质上是一个网站,将一些数据加载到 webview 等中,没有什么特别的。

我困扰的是应用程序生命周期及其“恢复”状态,应用程序已经在哪里运行,用户再次打开应用程序,然后重新渲染整个应用程序,所有 html+js 都到达再次运行,这基本上会导致重新加载(就像通过 F5 一样)。如何预防?

这是非常基本的 main.js 文件,请注意我将之前的执行状态与应用程序执行状态“正在运行”进行比较的行。我认为我应该做一些事情来防止重新加载我的应用程序(或者换句话说重新渲染),这样它就不会再次经历初始化过程。它看起来很糟糕,因为所有资源都被重新加载。

   (function () {
"use strict";

var app = WinJS.Application;
var activation = Windows.ApplicationModel.Activation;
var isFirstActivation = true;

app.onactivated = function (args) {
if (args.detail.kind === activation.ActivationKind.voiceCommand) {
// TODO: Handle relevant ActivationKinds. For example, if your app can be started by voice commands,
// this is a good place to decide whether to populate an input field or choose a different initial view.
}
else if (args.detail.kind === activation.ActivationKind.launch) {
// A Launch activation happens when the user launches your app via the tile
// or invokes a toast notification by clicking or tapping on the body.
if (args.detail.arguments) {
// TODO: If the app supports toasts, use this value from the toast payload to determine where in the app
// to take the user in response to them invoking a toast notification.
}
else if (args.detail.previousExecutionState === activation.ApplicationExecutionState.terminated) {
// TODO: This application had been suspended and was then terminated to reclaim memory.
// To create a smooth user experience, restore application state here so that it looks like the app never stopped running.
// Note: You may want to record the time when the app was last suspended and only restore state if they've returned after a short period.
}
else if (args.detail.previousExecutionState === activation.ApplicationExecutionState.running) {
isFirstActivation = false;
}
}

if (!args.detail.prelaunchActivated) {
// TODO: If prelaunchActivated were true, it would mean the app was prelaunched in the background as an optimization.
// In that case it would be suspended shortly thereafter.
// Any long-running operations (like expensive network or disk I/O) or changes to user state which occur at launch
// should be done here (to avoid doing them in the prelaunch case).
// Alternatively, this work can be done in a resume or visibilitychanged handler.
}

if (isFirstActivation) {
// TODO: The app was activated and had not been running. Do general startup initialization here.
document.addEventListener("visibilitychange", onVisibilityChanged);
args.setPromise(WinJS.UI.processAll().then(function(){ WinJS.UI.enableAnimations();}));
}

isFirstActivation = false;
};

function onVisibilityChanged(args) {
if (!document.hidden) {
// TODO: The app just became visible. This may be a good time to refresh the view.
}
}

app.oncheckpoint = function (args) {
// TODO: This application is about to be suspended. Save any state that needs to persist across suspensions here.
// You might use the WinJS.Application.sessionState object, which is automatically saved and restored across suspension.
// If you need to complete an asynchronous operation before your application is suspended, call args.setPromise().
};

app.start();

})();

最佳答案

好吧,经过一段时间的调试和重写应用程序前后,我意识到罪魁祸首是应用程序 list 中的“StartPage”。

应用程序即使之前处于运行状态也会被“重新加载”的问题是由于用户每次重新打开应用程序后都会触发“app.Start()”造成的。但 UWP 示例工具包中的示例并未受到此行为的影响,并且我注意到“app.Start()”仅在应用程序实际启动时被触发一次。随后的每个打开都不再像我的应用程序中那样达到该点(到“app.Start()”)。

以前,由于Content Security Policy,我将这个值设置为StartPage="ms-appx-web:///index.html"问题。但是将此值与示例进行比较,他们使用“StartPage=index.html”让我认为这可能是问题所在,我是对的。我花了几个小时来确保没有收到任何与内容安全策略相关的错误(替换了内联脚本),但它最终起作用了。

我无法解释为什么“StartPage='ms-appx-web:///index.html'”会导致应用程序每次重新启动,而“StartPage='index.html'”是正确维护其状态,但它确实解决了我的问题。

关于javascript - Windows UWP应用程序生命周期: how to prevent reload in JS/HTML/CSS app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39436947/

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