【问题标题】:Internet connection error message - Windows 8 Application with JavascriptInternet 连接错误消息 - 带有 Javascript 的 Windows 8 应用程序
【发布时间】:2012-11-25 19:38:46
【问题描述】:

我正在尝试为我的应用程序添加一个基本检测脚本,以允许它在调用 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();
}

当我显示对话框时出现问题。由于某些奇怪的原因,我在尝试显示对话框时收到“拒绝访问”错误消息。我检查了该消息的含义,如果我理解正确的话,它似乎是某个未兑现的承诺,尽管我看不出有任何方法可以将其应用于我的情况。

感谢任何有关此错误的帮助!

【问题讨论】:

    标签: javascript windows-8 winjs messagedialog


    【解决方案1】:

    您在这里面临的问题是您不能将多个消息对话框堆叠在一起——例如,您一次只能显示一个。我使用以下代码在我的应用程序中解决了这个问题:

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

    这会将它们排列起来并一个接一个地显示。显然,这不适用于您的情况,但这应该是一个很好的起点。 :)

    【讨论】:

    • 出于某种原因,虽然我们基本上有相同的代码:D 非常感谢!
    【解决方案2】:

    这里重要的是要有一个全局变量来确定对话框是否可见。 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
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      相关资源
      最近更新 更多