在用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
欢迎指正!谢谢