dialog.showMessageBox不返回电子main.js中的按钮索引 [英] dialog.showMessageBox not returning button index in electron main.js
问题描述
我有一个消息框,当用户在dashboardWindow上单击关闭时(Windows操作系统右上角的X按钮),该消息框将打开。
I have a messagebox that will open when the user click close on dashboardWindow (X button top right on windows os)
dashboardWindow.on("close", (event) => {
event.preventDefault();
console.log("before message box");
dialog.showMessageBox(
dashboardWindows,
{
message: "Test",
buttons: ["Default Button", "Cancel Button"],
defaultId: 0, // bound to buttons array
cancelId: 1 // bound to buttons array
},
(response) => {
if (response === 0) {
// bound to buttons array
console.log("Default button clicked.");
} else if (response === 1) {
// bound to buttons array
console.log("Cancel button clicked.");
}
}
);
console.log("after message box");
});
}
当我关闭dashboardWindow时,消息框已打开,但我却没有得到响应=== 0
即可正常工作。 Samehow console.log(消息框后);
已经运行,即使没有单击按钮也是如此。如何使响应有效(messageBox上的返回索引按钮)?
The messagebox opened when i close the dashboardWindow but i can't get response === 0
to work. Samehow console.log("after message box");
already run even when there is no click on the buttons. How I can make the response work (return index button on messageBox)?
推荐答案
请参阅有关以下内容的最新API文档: dialog.showMessageBox :此方法返回Promise对象,并且不使用不再有回调函数,就像以前一样,直到Electron v5.xx
Please refer to the most recent API doc about dialog.showMessageBox: this method returns a Promise object and doesn't make use of a callback function any more, like it used to until Electron v5.x.x.
返回
Promise< Object>
-做出包含
以下属性的承诺来解决:
Returns
Promise<Object>
- resolves with a promise containing the following properties:
-
响应
数值-单击按钮的索引。 -
checkboxChecked
布尔值-如果<$,复选框的选中状态设置了c $ c> checkboxLabel 。否则false
。
response
Number - The index of the clicked button.checkboxChecked
Boolean - The checked state of the checkbox ifcheckboxLabel
was set. Otherwisefalse
.
这应该可行然后(不过在您的上下文中未经测试):
This should work then (untested in your context though):
dashboardWindow.on("close", (event) => {
event.preventDefault();
console.log("before message box");
dialog.showMessageBox(
dashboardWindows,
{
message: "Test",
buttons: ["Default Button", "Cancel Button"],
defaultId: 0, // bound to buttons array
cancelId: 1 // bound to buttons array
})
.then(result => {
if (result.response === 0) {
// bound to buttons array
console.log("Default button clicked.");
} else if (result.response === 1) {
// bound to buttons array
console.log("Cancel button clicked.");
}
}
);
console.log("after message box");
});
这篇关于dialog.showMessageBox不返回电子main.js中的按钮索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!