给定一个应用程序,我在控制器类中有一个help按钮,它可以打开另一个包含帮助文档文件的BrowserWindow。当我首先关闭“帮助”窗口时,一切都正常。当我关闭原始窗口时,帮助窗口保持打开状态,然后当我关闭它时,它崩溃了,因为控制器本身已经被移除。我尝试过使用ipc消息的各种方法,但我认为有一种方法可以解决这个问题,但我还没有想出如何向所有打开的窗口广播“关闭”消息。当原始窗口关闭时,仅使用app.quit()或app.exit(status)关闭应用程序似乎也不会关闭帮助窗口。这里有一个片段
home.html
<button ng-click="_ctrl.showHelp()">Help</button>homeController.js
const {BrowserWindow} = require('electron').remote;
const {ipcRenderer} = require('electron');
export class HomeController {
constructor() {} //stuff
showHelp() {
let win = new BrowserWindow({
title: 'Help Pages',
nodeIntegration: false,
show: false
});
win.on('closed', () => {
win = null;
});
win.load(`${__dirname}/help.html`);
win.once('ready-to-show', () => {
win.show();
});
}
}index.js
const { app, ipcMain, ipcRenderer } = require('electron');
const { BrowserWindow } = require('electron').remote;
let mainWindow = null;
app.on('window-all-closed', () => {
app.quit();
}
app.on('ready', () => {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
show: false,
});
mainWindow.on('closed', () => {
mainWindow = null;
app.quit(); //neither of these work
app.exit(0); //neither of these work
});
mainWindow.loadURL(`file://${__dirname}/index.html`);
mainWindow.show();
});任何方向都将不胜感激。
发布于 2019-03-29 09:06:29
要解决此问题,您只需在index.js上具有帮助窗口的实例,使用帮助窗口实例,您可以设置关闭事件时的主窗口,以在关闭帮助窗口时将其关闭。
示例
const mainWindow = null;
const helpWindow = null;
mainWindow = new BrowserWindow()
mainWindow.on('close', function(){
helpWindow = null
mainWindow = null
})为了更好地实践,您可能希望将所有窗口存储在一个数组中,但为了简单起见,我这样做了。我希望我能帮到你。哦,只在渲染器上使用remote和ipcRenderers,ipcMain只在index.js上使用。
https://stackoverflow.com/questions/38000195
复制相似问题