Electron - IPC - 在窗口之间发送数据 [英] Electron - IPC - sending data between windows

查看:19
本文介绍了Electron - IPC - 在窗口之间发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在主进程中,我创建了一个名为 mainWindow 的窗口.单击按钮后,我创建了一个名为 notesWindow 的新 browserWindow.

我想要做的是将数据从 notesWindow 发送到 mainWindow

我所做的是使用 IPC 发送首先将数据从 notesWindow 发送到主进程,检索主进程上的数据,然后将该数据发送到 mainWindow,但 mainWindow 无法接收到 sender 事件.将数据发送到主进程可以正常工作,但从主进程到 browserWindow 似乎不起作用.

<块引用>

main.js

const ipcMain = require('electron').ipcMain;ipcMain.on('notes', function(event, data) {console.log(data)//这会正确显示数据event.sender.send('notes2', data);});

<块引用>

noteWindow.js

const ipcRenderer = require('electron').ipcRenderer;ipcRenderer.send('notes', "new note");

<块引用>

mainWindow.js

const ipcRenderer = require("electron").ipcRenderer;ipcRenderer.on('notes2', function(event, data) {//这个函数永远不会被调用控制台.log(数据);});

谁能解释我做错了什么?提前致谢!

解决方案

mainWindow 无法接收事件,因为它没有被发送给它.main.js 中的 events.sender.send() 代码会将数据发回给发送 notes 事件的人,在本例中是是 noteWindow.所以 notes2 事件被发送回 noteWindow 而不是 mainWindow.

要将 notes2 事件发送到 mainWindow,请查看 webContents.send().这允许主进程通过事件将数据发送到特定窗口.在对 main.js 进行一些修改后,它看起来类似于:

ipcMain.on('notes', function(event, data) {mainWindow.webContents.send('notes2', data);});

In the main process, I create a window called mainWindow. On a button click, I create a new browserWindow called notesWindow.

What I want to do is send data from notesWindow to mainWindow

What I did is used IPC send to first send the data from notesWindow to the main process, retrieve the data on the main process, then send that data to mainWindow, but mainWindow is unable to receive the sender event. Sending data to the main process works fine, but from the main process to browserWindow doesn't seem to work.

main.js

const ipcMain = require('electron').ipcMain;

ipcMain.on('notes', function(event, data) {
      console.log(data) // this properly shows the data
      event.sender.send('notes2', data);
});

noteWindow.js

const ipcRenderer = require('electron').ipcRenderer;
ipcRenderer.send('notes', "new note");

mainWindow.js

const ipcRenderer = require("electron").ipcRenderer;
ipcRenderer.on('notes2', function(event, data) {
    // this function never gets called
    console.log(data);
});

Can anyone explain what I'm doing wrong? Thanks in advance!

解决方案

mainWindow is not able to receive the event because it is not getting sent to it. The events.sender.send() code in main.js will send the data back to whoever sent the notes event, which in this case is the noteWindow. So the notes2 event is getting sent back to noteWindow instead of mainWindow.

To send the notes2 event to mainWindow, check out webContents.send(). This allows the main process to send data via events to a specific window. After some modifications to main.js it would look similar to this:

ipcMain.on('notes', function(event, data) {
    mainWindow.webContents.send('notes2', data);
});

这篇关于Electron - IPC - 在窗口之间发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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