我正在尝试编写我的第一个基于Electron Boilerplate的Electron应用程序。我试图从主要的Electron进程向我的窗口发送一条简单的消息,但似乎消息没有被发送。
我实现的主要代码如下:
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(目标映射到主窗口)
import { ipcRenderer } from "electron";
ipcRenderer.on('test', (event, text) => { console.log("Received test
message:", text)});
console.log(ipcRenderer);
有什么想法为什么事件没有被接收?我看到控制台日志显示DEV代码正在运行,但应用程序窗口侧面上没有任何内容(在开发者控制台日志中)。完整的代码可以在Git Repo找到。非常感谢您的帮助。
谢谢 Oliver