gpt4 book ai didi

electron - Electron 构建未执行开发项目的工作方式

转载 作者:行者123 更新时间:2023-12-03 12:37:56 25 4
gpt4 key购买 nike

我已经构建了一个Electron应用程序,并且正在尝试构建它。当我在开发环境中使用“npm start”运行它时,它可以完美运行,但是一旦构建完成,它就会失败。我已经尝试过用 Electron 伪造和 Electron 包装机。从本质上讲,我的应用程序位于任务栏中,并且每秒扫描一个文件夹。如果文件夹不为空,则显示窗口。显示窗口时,循环停止。一旦main.js文件收到用户操作已完成的命令,它将隐藏该窗口并返回到托盘以继续扫描。这是我的main.js文件:

const { app, BrowserWindow, Tray, Menu } = require('electron')
var ipcMain = require('electron').ipcMain;
const Store = require('electron-store');
const store = new Store();
const fs = require('fs');

shouldScan = true;

// global window declaration function
var win;
async function createWindow () {
win = new BrowserWindow({
width: 500,
height: 250,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
}
})
win.setMenuBarVisibility(false)
win.loadFile('index.html')

tray = new Tray('spongebob.ico')
const contextMenu = Menu.buildFromTemplate([
{
label: 'Show App', click: function () {
win.show()
}
},
{
label: 'Quit', click: function () {
app.isQuiting = true
app.quit()
}
}
])
tray.setToolTip('This is my application.')
tray.setContextMenu(contextMenu)

win.on('minimize', function (event) {
event.preventDefault()
shouldScan = true
scanning()
win.hide()
})

win.on('show', function (event) {
event.preventDefault()
shouldScan = false
})
await sleep(1000)
win.minimize()
}

// start the application
app.whenReady().then(createWindow)

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})

app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})

//allow delays
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}


//check the designated folder and stop the loop while showing the app window from the tray
async function scanning(){
while(shouldScan){
console.log('scanning')
if(store.get('default_path') != null){
files = fs.readdirSync(store.get('default_path'));
if(files.length > 0){
fs.rename(store.get('default_path') + "/" + files[0], store.get('default_path') + "/encounter", err => {
if (err) {
console.error(err)
return
}
})
console.log('should have shown')
win.show()
shouldScan = false
}
}
await sleep(1000)
}
}

//start the scanning funciton again when the signal is received
ipcMain.on('processInput', function(event, status) {
win.hide()
shouldScan = true
scanning()
});
我遇到的错误是窗口永远不会进入托盘。它甚至应该在启动后最小化1秒,但也没有这样做。实际上,除创建窗口外,主文件中的所有脚本均未运行。如果窗口最小化并且其目标文件夹不为空,则不会重新显示该窗口。窗口中的开发工具不会显示任何错误。我不知道如何在打包的.exe运行时运行命令提示符。有人建议吗?

最佳答案

对于将来的任何人,似乎 Electron 不仅仅喜欢本地文件路径。当我创建new Tray('spongebob.ico')时,效果不好。这样做似乎可以修复错误:

new Tray(path.join(__dirname, 'asset','img', 'spongebob.png'));
显然,我必须创建正确的路径和文件类型。

关于electron - Electron 构建未执行开发项目的工作方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65691120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com