Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

javascript - 在电子中关闭BrowserWindow时,事件侦听器继续引用BrowserWindow(When closing a BrowserWindow in electron, event listeners continue to reference the BrowserWindow)

My title probably doesn't properly capture my question, but I could not find a succinct way to capture the issue.(我的标题可能无法正确捕捉我的问题,但是我找不到捕捉问题的简洁方法。)

The issue is like this:(问题是这样的:) I have a BrowserWindow that is triggered by an IPC event.(我有一个由IPC事件触发的BrowserWindow。) When the BroswerWindow opens, it sends a message back to the main process to signal that it has completed loading, and then the main process sends it some data with which to perform a specific task.(当BroswerWindow打开时,它将向主进程发送一条消息,以表示已完成加载,然后,主进程向其发送一些数据以执行特定任务。) Upon completion of the task, the user closes the window and execution of a different process begins.(完成任务后,用户关闭窗口,并开始执行其他过程。) This all works fine, except that it the application receives that event to open the BrowserWindow again, the event handler that sends data to the new window either throws an error indicating that it cannot send data to the windows because the process has been destroyed OR reopens the window, but with all of the old data from the first time that the windows was opened, when what I need is a fresh instance of the window.(一切正常,除了应用程序收到该事件以再次打开BrowserWindow之外,将数据发送到新窗口的事件处理程序要么抛出错误,表明由于进程已被破坏或重新打开而无法将数据发送到窗口窗口,但包含第一次打开窗口时的所有旧数据,当时我需要的是窗口的新实例。) I know that I could simply use javascript to tear down and regenerate the original HTML, but I feel like there must be a better way.(我知道我可以简单地使用javascript拆除并重新生成原始HTML,但是我觉得必须有更好的方法。) Here is the code below:(这是下面的代码:) main.js(main.js) const electron = require ("electron"); const ipcMain = require('electron').ipcMain; const app = electron.app; const BrowserWindow = electron.BrowserWindow; function openNewWindow() { let win = new BrowserWindow({ autoHideMenuBar: true }); win.loadURL(__dirname + '\new_window.html'); win.webContents.openDevTools() win.on('closed', function () { win = null; }) return win }; let mainWindow; app.on('window-all-closed', function () { if (process.platform != 'darwin') { app.quit (); } }); app.on('ready', function () { mainWindow = new BrowserWindow ({ title: app.getName() + " - v" + app.getVersion(), autoHideMenuBar: true }); mainWindow.loadURL ('file://' + __dirname + '/index.html'); //This event handler opens the new window when it receives the open-new-window event ipcMain.on('open-new-window', (event,arg) => { console.log('Arg = ' + arg); let newWindow = openNewWindow(); //This event handler sends data to the new window when the new window indicates that it is done loading ipcMain.on('done-loading',(event2,arg2) => { console.log(arg2); newWindow.webContents.send('test',arg); }); }); // Close the application when the window is closed mainWindow.on ('closed', function() { mainWindow = null; }); }); 这是我在关闭新窗口后触发open-new-window事件时遇到的错误: The reference to line 52 in the error message is to this line:(错误消息中对第52行的引用是此行:) newWindow.webContents.send('test',arg); The new window opens, but no data is sent to it.(新窗口打开,但没有数据发送到该窗口。)   ask by Zachary Schwartz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The issue is similar to this question.(问题类似于问题。)

Every open-new-window event causes you to resubscribe ipcMain to the done-loading event for the new window, but it still maintains the subscription/closure to the old window.(每个open-new-window事件都会使您为open-new-window重新订阅ipcMaindone-loading事件,但是它仍然保持对旧窗口的订阅/关闭。) You do not want to do ipcMain.on("done-loading", ...) inside of the the new window handler.(您不想在新窗口处理程序内部执行ipcMain.on("done-loading", ...) 。) You want to do it outside of the window handler and instead send a response back to the same webcontents by using the event argument:(您想在窗口处理程序之外执行此操作,而是使用event参数将响应发送回相同的webcontents:) ipcMain.on('open-new-window', (event, arg) => { openNewWindow(); }); ipcMain.on('done-loading', (event, arg) => { event.sender.send('test', arg); }); However, there is a did-finish-load event that does what you seem to want to do:(但是,有一个did-finish-load事件可以完成您似乎想做的事情:) function openNewWindow() { let win = new BrowserWindow({ autoHideMenuBar: true }); win.loadURL(__dirname + '\new_window.html'); win.webContents.once("did-finish-load", () => { win.webContents.send("test", ...); }); };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...