说实话,我不知道发生了什么。
我一直在关注该图标,几分钟后它就消失了。不,它不会指向时钟附近的箭头:
这是我显示的图标(红色爆炸):
我不知道如何调试图标是否存在但为空,或者是否有事件触发它隐藏,或者托盘进程是否由于错误而自行关闭。控制台或我的应用程序中没有任何反应。
有人可以帮忙吗?下面是我的整个 index.js
:
const {app, BrowserWindow, Tray, Menu} = require('electron');
const path = require('path');
var win = '',
iconpath = path.join(__dirname, '/libs/img/icon.ico');
// Create the browser window
function createWindow () {
// BrowserWindow size
win = new BrowserWindow({
width: 800,
height: 720,
webPreferences: {
nodeIntegration: true
}
});
// tray menu
var contextMenu = Menu.buildFromTemplate([
{
label: 'Show app', click: function () {
win.show()
}
},
{
label: 'Quit', click: function () {
app.isQuiting = true;
app.quit();
}
}
]);
// Creates tray menu with tray icon
var appIcon = new Tray(iconpath);
// Define menu
appIcon.setContextMenu(contextMenu);
win.on('close', function () {
app.isQuiting = true;
app.quit();
});
// Load the index.html of the app
win.loadFile('./view/index.html');
}
app.on('ready', createWindow);
最佳答案
这是一个与垃圾收集相关的众所周知的问题,在Electron FAQ中提到过。页面:
My app's window/tray disappeared after a few minutes.
因此,一个快速解决方法是将 appIcon
变量的声明移出 createWindow
函数,位于 win
变量旁边例如:
const {app, BrowserWindow, Tray, Menu} = require('electron');
const path = require('path');
var win = '',
appIcon = null,
iconpath = path.join(__dirname, '/libs/img/icon.ico');
// Create the browser window
function createWindow () {
// BrowserWindow size
win = new BrowserWindow({
width: 800,
height: 720,
webPreferences: {
nodeIntegration: true
}
});
// tray menu
var contextMenu = Menu.buildFromTemplate([
{
label: 'Show app', click: function () {
win.show()
}
},
{
label: 'Quit', click: function () {
app.isQuiting = true;
app.quit();
}
}
]);
// Creates tray menu with tray icon
appIcon = new Tray(iconpath);
// Define menu
appIcon.setContextMenu(contextMenu);
win.on('close', function () {
app.isQuiting = true;
app.quit();
});
// Load the index.html of the app
win.loadFile('./view/index.html');
}
app.on('ready', createWindow);
关于javascript - NodeJS - Electron 托盘图标一分钟后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58594357/