js开发windows store中的MessageDialog

在用js开发windows store的过程中,如果你在弹出MessageDialog的时候,已经有一个MessageDialog被弹出来,这时候app就会crash,解决方案有两个:

1:把代码放在try{} catch{}里面,如下:

try{

    msg.showAsync();

}

catch(){


}

不建议用这种方式:

        1,try catch中的语句会单独进行处理.

        2,这个问题的根本原因没有解决。

2:写一个message dialog helper来对MessageDialog进行管理

MessageDialog: WinJS.Class.define(
            function constructor() {

            },
            {},
            {
                messageDialog: null,
                msgContent: "No internet connection has been found.",//set default content.
                msgTitle: "Information!",//set default title.
                isMessageDialogShow: false,
                showAsync: function () {//show message dialog.
                    this._initializeMessageDialog();
                    if (!this.isMessageDialogShow) {//show the message dialog if the isMessageDialogShow variable is false.
                        this.isMessageDialogShow = true;

                        var that = this;
                        return this.messageDialog.showAsync().done(function (args) {
                            that.isMessageDialogShow = false;
                        }, function (error) {
                            that.isMessageDialogShow = false;
                        });
                    }
                },              
                _initializeMessageDialog: function () {
                    //initialize the message dialog status.
                    if (this.messageDialog == null) {
                        this.messageDialog = new Windows.UI.Popups.MessageDialog(this.msgContent, this.msgTitle);

                        if (this.messageDialog.commands.length == 0)
                            this._appendDefaultCommand();//if the user haven't set the command then append default command.
                    }
                },
                _appendDefaultCommand: function () {
                    this.messageDialog.commands.append(new Windows.UI.Popups.UICommand("Go Back", (function onGobackInvoked() {
                        return function (args) {
                            if (WinJS.Navigation.canGoBack)
                                WinJS.Navigation.back();
                        }
                    })(), 0));
                    this.messageDialog.commands.append(new Windows.UI.Popups.UICommand("Close", null, 1));
                }
            })

我比较顷向于第二种做法,这种做法的关键是变量 isMessageDialogShow,用来指示MessageBox是否已经弹出。

demo下载地址:http://yunpan.cn/QDv879ZZE9Zxm

                                                                                                                                                                                                            欢迎指正!谢谢



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值