在WinJS应用程序中导航总是会引发异常

时间:2012-08-19 14:28:58

标签: javascript windows-8 winjs

每次尝试在页面的ready函数中执行导航时,应用程序都会崩溃。

具体而言,它在以下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);
        });
    }
}

任何人都知道为什么会这样吗?

2 个答案:

答案 0 :(得分:2)

您可以在这里找到几条路线:

  1. 抓住未处理的异常并忽略它
  2. 构建代码以避免设置错误条件
  3. 要忽略该错误,您可以设置一个可以处理未处理异常的WinJS.Application.onerror处理程序。这是一篇论坛帖子,可以指导您使用此解决方案:http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/686188b3-852d-45d5-a376-13115dbc889d

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

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

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

答案 1 :(得分:1)

如果您在发布预览版后在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

相关问题