Windows 10 个通用应用程序未从上一个会话恢复

Windows 10 universal app not resuming from previous session

我使用 Html、css 和 JS 开发了一个 windows 10 通用应用程序。为了允许内联脚本,我使用 ms-appx-web 上下文并将 ms-appx-web:///login.html 设置为清单中的起始页。 每当我在 windows 10 移动版中打开我的应用程序时,它都能正常工作,但如果我切换到另一个应用程序,然后通过从 windows 应用程序列表中选择它再次转到该应用程序。然后它不会从保存的状态恢复应用程序,而是重新启动它。

(function () {
    "use strict";

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

    app.onactivated = function (args) {
      if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState === activation.ApplicationExecutionState.terminated) {
            }
            if (WinJS.Application.sessionState.url) {
                localStorage.setItem("UserName", WinJS.Application.sessionState.name);
                window.location = WinJS.Application.sessionState.url;
            }
            args.setPromise(WinJS.UI.processAll().then(function () {
            }));
      }

    };

    app.oncheckpoint = function (args) {
        var location = window.location.href;
        var name = localStorage.getItem("UserName");
        WinJS.Application.sessionState.name = name;
        WinJS.Application.sessionState.url = location;
    };

    Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", function (args) {
        if (WinJS.Application.sessionState) {
            window.location = WinJS.Application.sessionState.url;
            localStorage.setItem("UserName", WinJS.Application.sessionState.name);
        }
    }, false);
    Windows.UI.WebUI.WebUIApplication.addEventListener("suspending", function (args) {
        var location = window.location.href;
        var name = localStorage.getItem("UserName");
        WinJS.Application.sessionState.name = name;
        WinJS.Application.sessionState.url = location;
    }, false);

    app.start();

})();

任何人都可以告诉我我做错了什么吗?

我在 main.js

更改了我的 app.onactivated 活动
    app.onactivated = function (args) {
    if (args.detail.kind === activation.ActivationKind.launch) {
        if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {

        } else {

        }
        args.setPromise(WinJS.UI.processAll());

        var name = Windows.Storage.ApplicationData.current.roamingSettings.values["name"];
        var url = Windows.Storage.ApplicationData.current.roamingSettings.values["url"];

        if (name) {
            localStorage.setItem("UserName", name);
        }
        if (url) {
            window.location.href = url;
        }
    }
};

但它在 window.location.href = url; 行停止 运行。

我想做的是在暂停事件中存储用户名和当前 url,并希望在恢复事件中恢复它(当用户从​​已经 运行 的应用程序列表中打开应用程序时)

but if I switch to another app and then go to app again by selecting it from windows app list. Then it instead of resuming app from saved state it restarts it.

我认为您没有为您的应用程序使用单页导航。

请参考Single-page navigation: the recommended model:

The script context is destroyed and must be initialized again. The app might receive system events but not handle them because the script context is being destroyed and reinitialized.

因此在您导航到其他页面后脚本上下文已经被销毁。

要解决此问题,最好的解决方案是使您的应用程序成为单页应用程序。并使用 PageControl. You can refer to Quickstart: Using single-page navigation 开始浏览页面。

更新:

but when I use window.location.href for redirecting in main.js it closes app.

因为你在WinJS脚本中使用了它。当您离开页面时,WinJS 脚本上下文将被破坏,因此执行内部代码会使应用程序崩溃。要解决此问题,您可以改用 windows 生命周期 API:

var roaming=Windows.Storage.ApplicationData.current.roamingSettings;

Windows.UI.WebUI.WebUIApplication.addEventListener("activated", function (args) {
    if (args.detail[0].kind === activation.ActivationKind.launch) {
        if (roaming.values["currentUri"]) {
            window.location.href = roaming.values["currentUri"];
        }
    }
});

Windows.UI.WebUI.WebUIApplication.addEventListener("suspending", function (args) {
    roaming.values["currentUri"] = window.location.href;
    roaming.values["UserName"] = evt.srcElement.value;
    //save the other information of the page here
});

Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", function (args) {
    var roam = Windows.Storage.ApplicationData.current.roamingSettings;
    if (roam) {
        if (roam["currentUri"])
        {
            window.location.href = roam["currentUri"];
        }
    }
}, false);

也可以参考我的demo.

注意:如果您根本不使用 WinJS,只需删除引用即可。在每个页面上加载 WinJS 库效率不高。

我已将 main.js 更改为:

    (function () {
"use strict";

//No need of WinJS
var activation = Windows.ApplicationModel.Activation;
var roaming = Windows.Storage.ApplicationData.current.roamingSettings;

// For App Start Up
Windows.UI.WebUI.WebUIApplication.addEventListener("activated", function (args) {
    if (args.detail[0].kind === activation.ActivationKind.launch) {
        if (roaming.values["currentUri"]) {
            if (roaming.values["UserName"])
            {
                localStorage.setItem("UserName", roaming.values["UserName"]);
                window.location.href = roaming.values["currentUri"];
            }
        }
    }
});

// For App Suspension
Windows.UI.WebUI.WebUIApplication.addEventListener("suspending", function (args) {
    roaming.values["currentUri"] = window.location.href;
    roaming.values["UserName"] = localStorage.getItem("UserName");
});

// For Resuming App
Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", function (args) {
    var roam = Windows.Storage.ApplicationData.current.roamingSettings;
    if (roam) {
        if (roam.values["currentUri"]) {
            localStorage.setItem("UserName", roam.values["UserName"]);
            window.location.href = roam.values["currentUri"];
        }
    }
}, false);
// not working backpressed event
Windows.UI.WebUI.WebUIApplication.addEventListener("backpressed", function (args) {
   // to do
}, false);})();

一切正常。但我不知道如何在不使用 winjs 的情况下添加返回按键事件。 谁能建议我?