問題描述
我知道有數百個問題,例如:我如何防止電子中的近距離事件"或類似的問題.
在 beforeunload
事件中實現確認框(電子消息框)后,我能夠關閉我的應用程序并取消關閉事件.由于開發工具始終處于打開狀態,我沒有意識到它在開發工具關閉時不起作用...
i know that there are hundreds of questions like: "How can i prevent close event in electron" or something like that.
After implementing a confirmation box (electron message box) in the beforeunload
event i was able to close my app and cancel the close event. Since the dev tools are always open, i didn't recognize that it doesn't work while the dev tools are closed...
window.onbeforeunload = e =>
{
// show a message box with "save", "don't save", and "cancel" button
let warning = remote.dialog.showMessageBox(...)
switch(warning)
{
case 0:
console.log("save");
return;
case 1:
console.log("don't save");
return;
case 2:
console.log("cancel");
return false;
// e.returnValue = "false";
// e.returnValue = false;
}
};
因此,當開發工具打開時,我可以通過保存關閉應用程序,而不保存并取消事件.
關閉開發工具后,取消按鈕不再起作用.
So, when the dev tools are opened, i can close the app with saving, without saving and cancel the event.
When the dev tools are closed, the cancel button doesn't work anymore.
順便說一句:
window.onbeforeunload = e =>
{
return false;
alert("foo");
};
將取消關閉事件,顯然不會顯示消息(無論開發工具是打開還是關閉)
will cancel the close event and obviously wouldn't show the message (doesn't matter if dev tools are open or closed)
window.onbeforeunload = e =>
{
alert("foo");
return false;
};
如果開發工具打開,按確定后將取消關閉事件,如果開發工具關閉,按確定后將關閉應用程序
will cancel the close event after pressing ok if dev tools are open and will close the app after pressing ok if dev tools are closed
我故意使用消息框的同步 api,在我寫這個問題時,我發現一個兩個窗口的應用程序 (new remote.BrowserWindow()
) 的行為與使用開發工具.
Intentionally i'm using the synchronous api of the message box and while i'm writing this question i figured out that a two windowed app (new remote.BrowserWindow()
) will behave exactly like with the dev tools.
有人知道我該如何解決這個問題嗎?
非常感謝提前
Has anyone an idea how i can resolve this problem?
Many thanks in advance
推薦答案
而不是 onbeforeunload
更喜歡使用事件 close
.從這個事件中,您將能夠在整個關閉過程完成之前捕獲關閉事件(事件 closed
).使用 close
,您將能夠控制并在需要完成關閉時停止.
Instead of onbeforeunload
prefer working with the event close
. From this event, you'll be able to catch the closing event before the whole closure process is completed (event closed
). With close
, you'll be able to take the control and stop whenever you need the completion of the closure.
這在您創建 BrowserWindow 時是可能的,最好是在主進程中:
This is possible when you create your BrowserWindow, preferably in the main process:
// Create the browser window.
window = new BrowserWindow({});
// Event 'close'
window.on('close', (e) => {
// Do your control here
if (bToStop) {
e.preventDefault();
}
})
// Event 'closed'
window.on('closed', (e) => {
// Fired only if you didn't called e.preventDefault(); above!
})
此外,請注意函數 e.preventDefault()
正在整個代碼中傳播.如果您需要回到 Electron 的自然行為,則需要將變量 e.defaultPrevented
切換為 false
.
In addition, be aware that the function e.preventDefault()
is spreading in the whole code. If you need to be back to the natural behaviour of Electron, you need to toggle the variable e.defaultPrevented
to false
.
實際上,似乎 e.preventDefault()
函數正在將變量 e.defaultPrevented
處理為 true
直到它發生任何變化.
Actually, it seems e.preventDefault()
function is handling the variable e.defaultPrevented
to true
until any change on it.
這篇關于在 Electron 中確認 beforeunload的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!