久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Electron 中確認 beforeunload

Confirm beforeunload in Electron(在 Electron 中確認 beforeunload)
本文介紹了在 Electron 中確認 beforeunload的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我知道有數百個問題,例如:我如何防止電子中的近距離事件"或類似的問題.
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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 国外成人在线视频网站 | 亚洲精品一二三 | 男女羞羞免费视频 | 日韩综合在线 | 国产精品一区二区不卡 | .国产精品成人自产拍在线观看6 | 久草视频观看 | 色欧美片视频在线观看 | 国产精品美女久久久久aⅴ国产馆 | 国产黄色在线 | 国产精品激情小视频 | 亚洲天堂av一区 | 天天插天天狠天天透 | 欧美一区二区三区在线播放 | 草草草网站 | 国产91在线视频 | 特级黄色毛片 | 91精品久久久久久久久 | 国产精品不卡 | 国产欧美精品区一区二区三区 | av中文在线 | 国产精品一码二码三码在线 | 成人精品视频 | 国产乱人伦精品一区二区 | 久久久久久国产 | 日韩在线免费看 | 免费毛片在线 | 国产小视频自拍 | 国产精品日产欧美久久久久 | 在线国产小视频 | 男女视频在线观看免费 | 国产99久久久国产精品下药 | 99pao成人国产永久免费视频 | 男女羞羞视频在线看 | 久久精品国产一区二区电影 | 欧美日韩免费一区二区三区 | 国产不卡在线 | 日本天天操 | 国产激情视频在线免费观看 | 国产精品a免费一区久久电影 | 国产精品不卡 |