dialog.showMessageBox 没有在 electron 中返回按钮索引 main.js
dialog.showMessageBox not returning button index in electron main.js
我有一个消息框,当用户单击 dashboardWindow 上的 close(windows os 右上角的 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");
});
}
当我关闭ose dashboardWindow 时消息框打开,但我无法response === 0
工作。同样 console.log("after message box");
已经 运行 即使没有点击按钮。我如何才能使响应起作用(return 消息框上的索引按钮)?
log on window close
请参阅关于 dialog.showMessageBox 的最新 API 文档:此方法 returns 一个 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 if checkboxLabel
was set. Otherwise false
.
这应该可以工作(尽管在您的上下文中未经测试):
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");
});
//BRFORE QUIT
mainWindow.on('close', (event) => {
event.preventDefault();
dialog.showMessageBox(mainWindow,{
message: "Are you sure you want to Close?",
type: "warning",
buttons: ["Exit" ,"Cancel"],
defaultId: 0,
title: "Confirm Close",
detail: "This will shutdown the application !!!"
}
).then((res) => {
console.log(res);
if(res.response === 0){
mainWindow.destroy();
}
});
});
//AFTER QUIT
mainWindow.on('closed', (event) => {
app.quit();
sqlModel.closeDB();
console.log("done");
});
我有一个消息框,当用户单击 dashboardWindow 上的 close(windows os 右上角的 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");
});
}
当我关闭ose dashboardWindow 时消息框打开,但我无法response === 0
工作。同样 console.log("after message box");
已经 运行 即使没有点击按钮。我如何才能使响应起作用(return 消息框上的索引按钮)?
log on window close
请参阅关于 dialog.showMessageBox 的最新 API 文档:此方法 returns 一个 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");
});
//BRFORE QUIT
mainWindow.on('close', (event) => {
event.preventDefault();
dialog.showMessageBox(mainWindow,{
message: "Are you sure you want to Close?",
type: "warning",
buttons: ["Exit" ,"Cancel"],
defaultId: 0,
title: "Confirm Close",
detail: "This will shutdown the application !!!"
}
).then((res) => {
console.log(res);
if(res.response === 0){
mainWindow.destroy();
}
});
});
//AFTER QUIT
mainWindow.on('closed', (event) => {
app.quit();
sqlModel.closeDB();
console.log("done");
});