1

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

具体来说,它在以下行失败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);
        });
    }
}

有谁知道为什么会这样?

4

2 回答 2

2

您可以在这里采取几条路线:

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

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

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

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

window.setImmediate(this.initialize.bind(this));
于 2012-08-23T00:00:10.730 回答
1

If your running your code on the RTM version after coming from the Release Preview this should sort your problem.

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);

You should probably take a look at the migration docs which details what the above is actually for and why: http://www.microsoft.com/en-us/download/details.aspx?id=30706

于 2012-08-19T15:03:03.667 回答