我有一个两个窗口的应用程序,第一个窗口是这样创建的:
mainWindow = new BrowserWindow({
width: 1024, // width of the window
height: 768, // height of the window
show: false, // don't show until window is ready
frame: false,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
}
})
在该用户可以提示打开第二个窗口之后,将调用以下功能import { remote } from "electron";
let dev = require('electron-is-dev');
const path = require('path')
const url = require('url')
let win
export default function createPopupWindow(currentLanguage) {
win = new remote.BrowserWindow({
width: 500,
height: 700,
frame: false,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
}
});
win.webContents.on('did-finish-load', () => {
win.webContents.send('message', { language: currentLanguage });
});
let indexPath
// Determine the correct index.html file
// (created by webpack) to load in dev and production
if (dev && process.argv.indexOf('--noDevServer') === -1) {
indexPath = url.format({
protocol: 'http:',
host: 'localhost:8080',
pathname: '/src/editWindow/index.html',
slashes: true
})
} else {
// todo
indexPath = url.format({
protocol: 'file:',
pathname: path.join(__dirname, 'index.jsx'),
slashes: true
})
}
// Load the index.html
win.loadURL(indexPath)
win.once('ready-to-show', () => {
win.show()
})
}
问题是从 Electron 8升级到9后,我无法调用以下命令关闭第二个窗口。我已经发现关闭事件被调用,但是窗口没有关闭。import { ipcRenderer, remote } from 'electron';
...
remote.getCurrentWindow().close();
提前致谢!干杯
最佳答案
我在 Electron 版9.1.2中也遇到了同样的问题,最后我找到了解决它的方法。关键是您不能将窗口直接保留在mainProcess上。
之前在main.js
中,我有一个变量来存储窗口对象,现在我将其更改为存储window.id,并定义一个辅助函数来通过windowId获取窗口。
前:
let mainWindow;
const createWindow = () => {
mainWindow = new BrowserWindow({
width: 300,
height: 440,
frame: false,
});
}
app.on("ready", createWindow);
后:let mainWindowId = 0
const createWindow = () => {
let mainWindow = new BrowserWindow({
width: 300,
height: 440,
frame: false,
});
mainWindowId = mainWindow.id
}
app.on("ready", createWindow);
const getMainWindow = () => {
return BrowserWindow.getAllWindows().filter(
(window) => window.id === mainWindowId
)[0];
};
关于reactjs - Electron 9 remote.getCurrentWindow()。close()不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63142365/