0

アプリケーションに基本的な検出スクリプトを追加して、呼び出しがWinJS.xhr失敗したときにエラー メッセージを表示できるようにしようとしています。

私はすべての WinHS.xhr に次の関数 onerror を呼び出してもらい (の 2 番目のパラメーターを使用.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();
}

ダイアログを表示すると問題が発生します。ダイアログを表示しようとすると、奇妙な理由で「アクセスが拒否されました」というエラーメッセージが表示されます。そのメッセージの意味を確認しましたが、正しく理解できていれば、どこかで約束が守られていないように見えますが、これを自分の状況に適用する方法はありません。

このエラーに関するヘルプは大歓迎です!

4

2 に答える 2

4

ここで直面している問題は、複数のメッセージ ダイアログを互いに重ねることができないことです。たとえば、一度に 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();
        }
    })();

これにより、それらがキューに入れられ、次々と表示されます。明らかにこれはあなたの場合にはうまくいきませんが、これは良い出発点になるはずです。:)

于 2012-11-26T01:10:44.553 に答える
0

ここで重要なことは、ダイアログが表示されるかどうかを決定するグローバル変数を持つことです。Dominic のコードは、配列からのメッセージを次々に表示するのに適していますが、例を使用してエラーを発生させずに 1 つのメッセージのみを表示する場合は、次のようになります。

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
    });
}
于 2014-07-31T08:36:59.793 に答える