- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在建立我的第一个 Electron 项目,但遇到一个托盘图标重复的问题。这里向您展示图片中发生了什么:
我想指出,我正在测试该应用程序。频繁启动和停止它。最终,这些图标确实减少为一个(Windows 10垃圾收集?)。但是,我仍然坚信这是异常行为。
该应用程序本身允许用户打开新窗口来监视事物。由于我不喜欢将“附加”窗口最小化到 Electron 中的位置,因此我将它们最小化时将其设置为“隐藏”。这样的想法是,当用户想要再次显示该窗口时,可以在列表上右键单击系统托盘中的应用程序图标并选择所需的窗口名称,然后从列表中选择它。
我相信这个问题可能与我在更新它时创建和销毁任务栏图标的方式有关。我要做的是销毁托盘,然后在将新窗口名称附加到模板数组之后再次构建它(如下面的代码所示)。
这样做是一个好主意吗? -我还没有看到很多如何做到这一点的例子,所以我自己动手做。
如果您需要更多信息,请随时在下面评论。谢谢!
相关代码:(在 main.js 中)
const iconPath = path.join(__dirname, '/images/testIcon.png')
let tray = null;
function ShowWindow(windowNameFromTray)
{
singleWindow.webContents.send('open-window-from-other-process', windowNameFromTray);
}
ipcMain.on('open-currently-open-window', function(e, windowName)
{
ShowWindow(windowName)
})
let template =
[
{
label: 'windows',
submenu:[]
}
]
ipcMain.on('retrieved-windowId', function(e, windowName)
{
tray.destroy()
tray = new Tray(iconPath)
tray.setToolTip('Window App')
var element =
{
label: windowName,
click()
{
ShowWindow(windowName)
}
}
template[0].submenu.push(element)
let contextMenu = Menu.buildFromTemplate(template)
tray.setContextMenu(contextMenu)
});
...
最佳答案
您好,我也不知道这对您也有同样的问题,但是我发现这与托盘的销毁有关,首先我们必须在main.js文件中创建一个Tray对象,然后我们必须使用该对象全局范围中的纸盘对象。现在我们做错的是我们正在创建任务栏图标,我们没有在破坏其他东西,而是再次创建新东西,现在我的解决方案是在每个地方都共享一个Tray对象,这样我们就可以破坏同一个Tray对象,所以它不会当我们创建一个新文件时创建一个新文件这是我的代码,我正在另一个文件中创建我的纸盘
Main.js
const electron = require("electron");
let app = null;
let tray = null;
const { app, BrowserWindow, ipcMain,Tray, Menu,dialog} = electron;
const nativeImage = electron.nativeImage
global.share = { app, BrowserWindow, ipcMain,Tray,tray,BackGroudProecess, Menu,mainwin,nativeImage};
const Mytray= require('./tray.js');
Mytray.Create(languagerPack);//you don't need to pass the language pack in here this code according to my developement (obivously you get that ;-) )
tray.js
module.export ={
CreateTray(items){
let p = new Promise((res,rej)=>{
const iconPath = path.join(__dirname+"../../../img/icon.ico");
global.share.tray = new Tray(global.share.nativeImage.createFromPath(iconPath))
res(global.share.tray);//this is the important palce we have pass global tray
})
p.then((tray)=>{
const trayMenuTemplate = [
{
label:items.not_signed,
enabled: false
},
{
label:items.about,// 'About',
click: function () {
global.share.mainwin.webContents.send("OPEN:ABOUT",{state:true});
}
},
{
label:items.help,// 'Help',
click: function () {
console.log("Clicked on Help")
}
},
]
let trayMenu = global.share.Menu.buildFromTemplate(trayMenuTemplate)
if(tray != null){
tray.setContextMenu(trayMenu)
}
}).catch((rej)=>{console.log(rej)})
},
}
}
如何销毁?
global.share.tray.destroy() // this way it destroys the correct tray since there is only one tray object in the whole program
而当您破坏和重新装上新托盘时,必须确保旧托盘是否已损坏,这就是您的处理方式
if(global.share.tray.isDestroyed){
console.log("Is Destroyed");
}
关于javascript - Electron 托盘图标重复问题(Windows 10),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62935881/
我想在同一运行时间内拥有多种货币。默认节点模板中插入了 Balances 托盘,但如果我正确的话,它只能处理一种货币。 如何多次重复使用托盘? 最佳答案 Pallet 可以实例化,这样您就可以在同一运
我正在尝试使用一段代码打开和关闭计算机的 CD 托盘。我一直在使用 MCI 命令,并将 winmm.lib 包含在我的项目配置的附加依赖项中。我还包含了 windows.h 和 mmsystem.h。
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我正在使用electron-react-boilerplate开发 Electron 应用程序(使用electron-builder打包应用程序)。 我想创建托盘,但是它需要图标路径或 native
有没有办法选择在 Java 中打印时使用哪个出纸盒/托盘?有一个属性 MediaTray 允许您选择输入托盘,但我找不到允许您选择输出的属性。 最佳答案 除了MediaTray , 有一个 Desti
我是一名优秀的程序员,十分优秀!