在 WinJS 应用中导航始终会引发异常
Navigating in a WinJS app always throws an exception
每次我尝试在页面的就绪功能中执行导航时,应用程序都会崩溃。
具体来说,它在下面的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);
});
}
}
有人知道为什么会这样吗?
这里有几条路线可以走:
- 捕获未处理的异常并忽略它
- 构建代码以避免设置错误条件
若要忽略该错误,可以设置一个 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
- WinJS内联绑定语法
- WinJS(WP8.1):从后台任务更新辅助磁贴
- 在javaservlet doPost方法中启动线程时,无法返回异常消息
- document.applet.method在Mounatin Lion上抛出safari 6+JDK7异常
- 为什么浏览器没有为语法错误抛出异常
- java,javascript签名的小程序(文件选择,ftp上传)安全性/套接字异常
- node.js和express中的异常处理
- 派生进程的stdout在管道传输时工作异常
- NodeJS和pg promise,捕获PostgreSQL异常
- 当显式定义控制器参数时,默认模型绑定器会发生异常
- 显示引导弹出操作异常的逻辑
- 调用谷歌地图路线服务时未捕获的异常
- web配置http到https重定向异常
- 转换异常时间&Datestring到JavaScript日期
- 为什么在python中使用异常比javascript更容易接受
- PHP REST服务抛出异常——希望避免控制台错误
- JS异常:animate不是一个函数
- 在 WinJS 应用中导航始终会引发异常
- WinJS 中未经处理的异常
- WinJS承诺导致base.js中的JavaScript异常