Javascript 在WinJS应用程序中导航总是引发异常
每次我尝试在页面的就绪功能中执行导航时,应用程序都会崩溃 具体来说,它在Javascript 在WinJS应用程序中导航总是引发异常,javascript,windows-8,winjs,Javascript,Windows 8,Winjs,每次我尝试在页面的就绪功能中执行导航时,应用程序都会崩溃 具体来说,它在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, op
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);
});
}
}
有人知道为什么会发生这种情况吗?如果您在发布预览后在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);
您可能应该看一看迁移文档,其中详细说明了上述内容的实际用途和原因:您可以在这里采取以下几种方法:
捕获未处理的异常并忽略它
构造代码以避免设置错误条件
要忽略该错误,可以设置一个WinJS.Application.OneError处理程序,该处理程序可以处理未处理的异常。以下是一篇论坛帖子,可以指导您使用此解决方案:
总的来说,我认为你最好一起避免例外情况。为此,这里发生的是一次只能发生一个导航事件(promise)。当您在ready函数中时,用于导航到groupedItems的导航承诺仍在运行。当您调用initialize时,它会调用WinJS.Navigation.navigate(“/pages/login/login.html”,{});它看到这一点,并尝试首先取消当前运行的导航承诺,这将导致您看到的异常
相反,您可以使用该函数设置对initialize()的调用,以在当前脚本块退出后运行。为此,将对initialize()的调用替换为:
尝试了你的代码,但不幸的是它没有工作。另外,我在一个全新的RTM项目中也体验到了这一点。同时检查视图中的文本数据绑定。不,不幸的是,调试器中除了terminateAppHandler
函数中的data.description
属性之外没有其他信息,该函数是{“description”:“cancelled”,“name”:“cancelled”
window.setImmediate(this.initialize.bind(this));