我有一个消息框,当用户在 dashboardWindow 上单击关闭(Windows 操作系统右上角的 X 按钮)时,该消息框将打开
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 时消息框打开,但我无法使 response === 0
工作。 Samehow console.log("after message box");
即使没有点击按钮也已经运行。我如何使响应起作用(消息框上的返回索引按钮)?
最佳答案
关于dialog.showMessageBox请引用最新的API文档:此方法返回一个 Promise 对象,并且不再像以前那样使用回调函数,直到 Electron v5.x.x。
Returns
Promise<Object>
- resolves with a promise containing the following properties:
response
Number - The index of the clicked button.checkboxChecked
Boolean - The checked state of the checkbox ifcheckboxLabel
was set. Otherwisefalse
.
这应该可以工作(尽管在您的上下文中未经测试):
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");
});
关于javascript - dialog.showMessageBox 不返回 Electron main.js 中的按钮索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59349045/