問題描述
我有一個消息框,當(dāng)用戶單擊dashboardWindow 上的關(guān)閉時會打開一個消息框(Windows 操作系統(tǒng)右上角的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");
});
}
當(dāng)我關(guān)閉 dashboardWindow 時消息框打開,但我無法讓 response === 0
工作.即使沒有點擊按鈕,console.log("after message box");
也已經(jīng)運行.我怎樣才能做出響應(yīng)(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)?
登錄窗口關(guān)閉
推薦答案
請參考最新的 API 文檔關(guān)于 dialog.showMessageBox:此方法返回一個 Promise 對象并且不再使用回調(diào)函數(shù),就像在 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.
Returns Promise
- 使用包含以下屬性:
Returns
Promise<Object>
- resolves with a promise containing the following properties:
response
Number - 點擊按鈕的索引.checkboxChecked
布爾值 - 如果設(shè)置了checkboxLabel
,則復(fù)選框的選中狀態(tài).否則false
.
response
Number - The index of the clicked button.checkboxChecked
Boolean - The checked state of the checkbox ifcheckboxLabel
was set. Otherwisefalse
.
這應(yīng)該可以工作(盡管在您的上下文中未經(jīng)測試):
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");
});
這篇關(guān)于dialog.showMessageBox 不返回電子 main.js 中的按鈕索引的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!