IPC通信在电子与窗口之间不起作用
我正在尝试编写基于Electron Boilerplate的第一个Electron应用程序。我正在尝试将一个简单的消息从Electron主进程发送到我的窗口中,但是似乎该消息没有发送出去。
I am trying to write my first Electron app based on Electron Boilerplate. I am trying to send a simple message from the main Electron process into my window but it seems that the message is not getting send.
我介绍的主要代码如下
background.js(主要Electron流程
// Window setup
app.on("ready", () => {
mainWindow = new BrowserWindow({
width: 1000,
height: 300,
frame: false,
resizable: false,
transparent: true,
});
mainWindow.setIgnoreMouseEvents(true);
mainWindow.hide();
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, "app.html"),
protocol: "file:",
slashes: true
})
);
const ret = globalShortcut.register(getKeyboardShortCut(), () => {
mainWindow.isVisible ? mainWindow.hide() : mainWindow.show();
})
if(isDev()){
mainWindow.openDevTools();
mainWindow.setIgnoreMouseEvents(false);
console.log("======== DEV ==========");
mainWindow.show();
mainWindow.webContents.send('test','This is a test');
}
});
app.js(映射到mainWindow的窗口)
import { ipcRenderer } from "electron";
ipcRenderer.on('test', (event, text) => { console.log("Received test
message:", text)});
console.log(ipcRenderer);
任何不知道为何未收到活动的想法吗?我在控制台日志中看到DEV代码正在运行,但在应用程序窗口侧却没有任何内容(在开发人员控制台日志中)完整代码可在 Git Repo
Any idea why the event is not getting received ? I see the console log that the DEV code is running but nothing on the app window side ( In the Developer console log ) The full code can be found at Git Repo
任何帮助将不胜感激。
感谢
Oliver
Thanks Oliver
如文档所示( https://github.com/electron/electron/blob/master /docs/api/web-contents.md#contentssendchannel-arg1-arg2- ),在渲染器准备好监听之后发送消息很重要。
As document indicates (https://github.com/electron/electron/blob/master/docs/api/web-contents.md#contentssendchannel-arg1-arg2-), It is important to send message once renderer is ready to listen.
if(isDev()){
mainWindow.openDevTools();
mainWindow.setIgnoreMouseEvents(false);
console.log("======== DEV ==========");
mainWindow.show();
// send after did-finish-load
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.send('test','This is a test');
})
}