在 WinJS 应用中导航始终会引发异常

Navigating in a WinJS app always throws an exception

本文关键字:异常 WinJS 应用 导航      更新时间:2023-09-26

每次我尝试在页面的就绪功能中执行导航时,应用程序都会崩溃。

具体来说,它在下面的WinJS.Navigation.navigate("/pages/login/login.html", {});行失败:

// This function is called whenever a user navigates to this page. It
// populates the page elements with the app's data.
ready: function (element, options) {            
    var listView = element.querySelector(".groupeditemslist").winControl;
    listView.groupHeaderTemplate = element.querySelector(".headertemplate");
    listView.itemTemplate = element.querySelector(".itemtemplate");
    listView.oniteminvoked = this._itemInvoked.bind(this);
    // Set up a keyboard shortcut (ctrl + alt + g) to navigate to the
    // current group when not in snapped mode.
    listView.addEventListener("keydown", function (e) {
        if (appView.value !== appViewState.snapped && e.ctrlKey && e.keyCode === WinJS.Utilities.Key.g && e.altKey) {
            var data = listView.itemDataSource.list.getAt(listView.currentItem.index);
            this.navigateToGroup(data.group.key);
            e.preventDefault();
            e.stopImmediatePropagation();
        }
    }.bind(this), true);
    this._initializeLayout(listView, appView.value);
    listView.element.focus();
    initialize();
}
function initialize() {
    // Check if user is logged in
    if (is_logged_in !== true) {
        WinJS.Navigation.navigate("/pages/login/login.html", {});
    }
    else {
        // TODO: Replace the data with your real data.
        // You can add data from asynchronous sources whenever it becomes available.
        generateSampleData().forEach(function (item) {
            list.push(item);
        });
    }
}

有人知道为什么会这样吗?

这里有几条路线可以走:

  1. 捕获未处理的异常并忽略它
  2. 构建代码以避免设置错误条件

若要忽略该错误,可以设置一个 WinJS.Application.onerror 处理程序来处理未处理的异常。以下是指导您使用此解决方案的论坛帖子:http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/686188b3-852d-45d5-a376-13115dbc889d

一般来说,我会说你最好一起避免例外。为此 - 这里发生的事情是一次只能发生一个导航事件(承诺)。当您在就绪函数内时,用于导航到分组项的导航承诺仍在运行。当你调用initialize时,它会调用WinJS.Navigation.navigate("/pages/login/login.html", {});它看到这一点,并尝试首先取消当前正在运行的导航承诺,这会导致你看到的异常。

相反,您可以使用 window.setImmediate 函数将 initialize() 的调用设置为在当前脚本块退出后运行。为此,请将对 initialize() 的调用替换为:

window.setImmediate(this.initialize.bind(this));

如果您在发布预览版之后在 RTM 版本上运行代码,这应该可以解决您的问题。

function initialize() {
    // Check if user is logged in
    if (is_logged_in !== true) {
        WinJS.Navigation.navigate("/pages/login/login.html", {});
    }
    else {
        // TODO: Replace the data with your real data.
        // You can add data from asynchronous sources whenever it becomes available.
        generateSampleData().forEach(function (item) {
            list.push(item);
        });
    }
}
var markSupportedForProcessing = WinJS.Utilities.markSupportedForProcessing;
var requireSupportedForProcessing = WinJS.Utilities.requireSupportedForProcessing;
markSupportedForProcessing(initialize);
requireSupportedForProcessing(initialize);

您可能应该查看迁移文档,其中详细说明了上述实际用途以及原因:http://www.microsoft.com/en-us/download/details.aspx?id=30706