Electron dialog.showOpenDialog() 关闭 Ubuntu 18.10 上的应用程序
Posted
技术标签:
【中文标题】Electron dialog.showOpenDialog() 关闭 Ubuntu 18.10 上的应用程序【英文标题】:Electron dialog.showOpenDialog() closes app on Ubuntu 18.10 【发布时间】:2019-05-30 08:34:11 【问题描述】:我正在尝试在 Ubuntu 18.10 上使用 Electron dialog.showOpenDialog(),但该方法 - 使用时 - 会立即关闭基于电子的应用程序。
dialog.showOpenDialog() 的简单使用会导致应用崩溃。当我将其注释掉时,该应用程序运行良好。当我在 Windows 上使用相同的代码时,它可以工作。
这里有没有人有一些提示如何专门为 Ubuntu 更改代码?
我尝试在主进程和渲染器进程中都使用该方法,但没有成功。 本机对话框打开片刻,然后整个应用程序退出。
在主进程中:
const dialog = require('electron')
...
dialog.showOpenDialog(
title: "Select a file",
filters: [ name: "All Files", extensions: ["*"] ]
,
fileNames =>
if (fileNames === undefined)
console.log("No file selected");
...
)
...
编辑:我尝试了一个非常简单的示例:
// Modules to control application life and create native browser window
const app, BrowserWindow, ipcMain, dialog = require('electron');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the javascript object is garbage collected.
let mainWindow
function createWindow()
// Create the browser window.
mainWindow = new BrowserWindow(
width: 800,
height: 600,
webPreferences:
nodeIntegration: true
)
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function ()
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
)
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', function ()
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
//if (process.platform !== 'darwin') app.quit()
)
app.on('activate', function ()
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) createWindow()
)
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
ipcMain.on('openFile', (event, path) =>
dialog.showOpenDialog(function (fileNames)
// fileNames is an array that contains all the selected
if (fileNames === undefined)
console.log("No file selected");
else
console.log("fileNames", fileNames);
);
)
【问题讨论】:
能贴一下整个主流程代码吗? 我会尝试注释掉filters
行,只是为了进行实际检查。
您使用的是哪个 Electron 版本? IE。 npm list electron
的输出是什么?
谢谢。 @Sharvink,另一个示例添加到问题中。该应用程序仍会立即关闭。
@NoGrabbing - 我尝试了各种选项,但结果还是一样。
【参考方案1】:
尝试使用默认的 Ubuntu 终端运行电子应用程序。我使用的是 VS Code 终端,但根据this issue on Github 发现它有问题
【讨论】:
以上是关于Electron dialog.showOpenDialog() 关闭 Ubuntu 18.10 上的应用程序的主要内容,如果未能解决你的问题,请参考以下文章
用electron将Vue项目打包为window和Mac桌面应用 (electron-packager与electron-build)