在Electron中确认前卸载 [英] Confirm beforeunload in Electron

查看:824
本文介绍了在Electron中确认前卸载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有数百个问题,例如:如何防止电子中的闭合事件或类似的问题。

在<$ c中实现确认框(电子消息框)后$ c> 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.

Btw .:

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;
};

如果打开了开发工具,则按ok后将取消关闭事件,按ok后将关闭应用程序如果开发工具已关闭

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()正在整个代码中扩展。如果需要回到电子的自然行为,则需要将变量 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中确认前卸载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆