2017-01-06 131 views
0

我有一个渲染器进程,我们称之为RP。 单击按钮时,它会打开浏览器窗口(BW)。无法阻止电子窗口关闭

现在当关闭点击BW时,我想在RP中捕捉这个关闭事件并阻止它。

在RP中调用close事件之前,我正在经历窗口关闭的情况。

代码:

bw.on("close", (e: Electron.Event) => hideWindow(e)); 
let hideWindow = (e: Electron.Event) => { 
    e.preventDefault(); 
    bw.hide(); 
    return false; 
    } 

我在做什么错?

我知道,在bw中我可以使用beforeunload,它的工作原理。但是我想让RP控制窗口是否关闭。

回答

0

这是不可能的,因为打开浏览器窗口的进程是渲染器进程,它通过electron.remote调用,因此被处理为异步。正因为如此,窗口在关闭事件处理之前关闭。 如果打开浏览器窗口的过程是主要过程,那就没问题。

这说明情况:https://github.com/CThuleHansen/windowHide 这里是问题的一个较长的讨论:https://discuss.atom.io/t/close-event-for-window-being-fired-after-window-has-been-closed/37863/4

2

这里是你如何防止关闭:

大量的试验后,我想出了一个解决方案:

// Prevent Closing when work is running 
window.onbeforeunload = (e) => { 
    e.returnValue = false; // this will *prevent* the closing no matter what value is passed 

    if(confirm('Do you really want to close the application?')) { 
    win.destroy(); // this will bypass onbeforeunload and close the app 
    } 
}; 

在文档实测值:event-closedestroy

+0

需要在渲染器中应用。 IE浏览器。在html(index.html)中。 – ryanjones