久久久久久久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?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 国产免费视频 | 免费视频成人 | 91看黄 | 成人免费视频视频 | 五月婷婷在线视频 | 亚洲深夜福利 | 精品日韩一区二区三区 | 日韩av不卡在线 | 国产成人精品亚洲男人的天堂 | 欧美三级大片 | 国产精品久久久国产盗摄 | 欧美一道本 | 视频爱爱免费视频爱爱太爽 | 一本色道久久综合亚洲精品酒店 | 中文字幕亚洲天堂 | 国产视频一区二区在线 | 国产理论在线观看 | 亚洲成人一区二区三区 | 91精品久久久久 | 三级视频在线 | 国产精品久久久久久久免费看 | 日本在线小视频 | 国产天堂在线 | 黄色片视频免费 | 91调教打屁股xxxx网站 | 亚洲一级黄色片 | 欧美在线视频免费 | 国产一级免费视频 | 黄色午夜 | 精品国产成人 | 亚洲男人天堂网 | 午夜免费网站 | 亚洲国产精品suv | 亚洲视频区 | 国产精品偷乱一区二区三区 | 中文天堂在线观看 | 超碰av在线播放 | 性史性dvd影片农村毛片 | 波多野结衣之双调教hd | 国产高清一区二区三区 | 色女人影院 |