Internet连接错误消息-使用Javascript的Windows 8应用程序
我正在尝试为我的应用程序添加一个基本的检测脚本,以允许它在调用Internet连接错误消息-使用Javascript的Windows 8应用程序,javascript,windows-8,winjs,messagedialog,Javascript,Windows 8,Winjs,Messagedialog,我正在尝试为我的应用程序添加一个基本的检测脚本,以允许它在调用WinJS.xhr失败时显示一条错误消息 我让每个WinHS.xhr调用以下函数onerror(使用.done()的第二个参数),这部分工作得很好 function connectionError() { var msg = new Windows.UI.Popups.MessageDialog("There was a connection error while trying to access online conten
WinJS.xhr
失败时显示一条错误消息
我让每个WinHS.xhr调用以下函数onerror(使用.done()
的第二个参数),这部分工作得很好
function connectionError() {
var msg = new Windows.UI.Popups.MessageDialog("There was a connection error while trying to access online content. Please check your internet connection.");
// Add commands and set their CommandIds
msg.commands.append(new Windows.UI.Popups.UICommand("Retry", null, 1));
// Set the command that will be invoked by default
msg.defaultCommandIndex = 1;
// Show the message dialog
msg.showAsync();
}
当我显示对话框时,问题出现了。出于某种奇怪的原因,我在尝试显示对话框时收到一条“拒绝访问”的错误消息。我检查了这条信息的含义,如果我理解正确的话,它似乎是某个地方未完成的承诺,尽管我看不到任何方法可以将其应用于我的情况
任何有关此错误的帮助都将不胜感激 您在这里面临的问题是,您不能将多个消息对话框堆叠在彼此的顶部,例如,一次只能显示一个。我在应用程序中使用以下代码解决了此问题:
(function () {
var alertsToShow = [];
var dialogVisible = false;
function showPendingAlerts() {
if (dialogVisible || !alertsToShow.length) {
return;
}
dialogVisible = true;
(new Windows.UI.Popups.MessageDialog(alertsToShow.shift())).showAsync().done(function () {
dialogVisible = false;
showPendingAlerts();
})
}
window.alert = function (message) {
if (window.console && window.console.log) {
window.console.log(message);
}
alertsToShow.push(message);
showPendingAlerts();
}
})();
这将使它们排队并依次显示。显然,这对您的案例不起作用,但这应该是一个很好的起点。:) 您在这里面临的问题是,您不能将多个消息对话框堆叠在彼此的顶部,例如,一次只能显示一个。我在应用程序中使用以下代码解决了此问题:
(function () {
var alertsToShow = [];
var dialogVisible = false;
function showPendingAlerts() {
if (dialogVisible || !alertsToShow.length) {
return;
}
dialogVisible = true;
(new Windows.UI.Popups.MessageDialog(alertsToShow.shift())).showAsync().done(function () {
dialogVisible = false;
showPendingAlerts();
})
}
window.alert = function (message) {
if (window.console && window.console.log) {
window.console.log(message);
}
alertsToShow.push(message);
showPendingAlerts();
}
})();
这将使它们排队并依次显示。显然,这对您的案例不起作用,但这应该是一个很好的起点。:) 这里重要的一点是使用一个全局变量来确定对话框是否可见。Dominic的代码很适合一个接一个地显示来自数组的消息,但如果您想使用示例仅显示一条消息而不出现错误,则应该如下所示:
var dialogVisible = false;
function connectionError() {
var msg = new Windows.UI.Popups.MessageDialog("There was a connection error while trying to access online content. Please check your internet connection.");
if (dialogVisible) {
return;
}
dialogVisible = true;
// Add commands and set their CommandIds
msg.commands.append(new Windows.UI.Popups.UICommand("Retry", null, 1));
// Set the command that will be invoked by default
msg.defaultCommandIndex = 1;
// Show the message dialog
msg.showAsync().done(function (button) {
dialogVisible = false;
//do whatever you want after dialog is closed
//you can use 'button' properties to determine which button is pressed if you have more than one
});
}
这里重要的是要有一个全局变量来确定对话框是否可见。Dominic的代码很适合一个接一个地显示来自数组的消息,但如果您想使用示例仅显示一条消息而不出现错误,则应该如下所示:
var dialogVisible = false;
function connectionError() {
var msg = new Windows.UI.Popups.MessageDialog("There was a connection error while trying to access online content. Please check your internet connection.");
if (dialogVisible) {
return;
}
dialogVisible = true;
// Add commands and set their CommandIds
msg.commands.append(new Windows.UI.Popups.UICommand("Retry", null, 1));
// Set the command that will be invoked by default
msg.defaultCommandIndex = 1;
// Show the message dialog
msg.showAsync().done(function (button) {
dialogVisible = false;
//do whatever you want after dialog is closed
//you can use 'button' properties to determine which button is pressed if you have more than one
});
}
虽然我们基本上有相同的代码,但出于某种原因,这是可行的:D非常感谢!虽然我们基本上有相同的代码,但出于某种原因,这是可行的:D非常感谢!