我有一个用HTML / JS / CSS构建的简单UWP应用程序。它本质上是一个网站,它将一些数据加载到webview等等,没什么特别的。
我挣扎的是应用程序生命周期及其“恢复”状态,应用程序已经在哪里运行,用户再次打开应用程序,然后重新呈现整个应用程序,所有html + js都到达再次运行导致基本上重新加载(就像通过F5)。我该如何预防?
这是非常基本的main.js文件,请注意我将上一个执行状态与应用程序执行状态“running”进行比较的行。我认为我应该做些什么来防止重新加载我的应用程序(或换句话说重新渲染),所以它不会再次经历初始化过程。它看起来很糟糕,因为所有资源都被重新加载。
(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();
})();
答案 0 :(得分:1)
好的,经过一段时间的调试并重新编写应用程序前后,我已经意识到这里的罪魁祸首是" StartPage"在应用程序清单中。
应用程序出现问题"重新加载"即使它之前处于运行状态是由" app.Start()"每次用户重新打开应用程序后触发。但来自UWP样品套件的样品并没有受到这种行为的影响,我已经注意到" app.Start()"当应用程序实际启动时,仅触发一次。每个后续的开放都不再像我的应用程序那样(到#34; app.Start()")。
以前,我有此值设置StartPage =" ms-appx-web:///index.html"由于Content Security Policy问题。但是将这个值与样本进行比较,他们使用" StartPage = index.html"让我觉得这可能是问题而我是对的。我花了几个小时来确保我没有得到任何与内容安全策略相关的错误(内联脚本被替换),但它最终有效。
我无法解释为什么" StartPage =' ms-appx-web:///index.html'"导致应用程序每次重新启动,而" StartPage =' index.html'"正确地保持其状态,但它确实解决了我的问题。