【问题标题】:Electron showOpenDialog arrow function (event.send) not workingElectron showOpenDialog 箭头函数(event.send)不起作用
【发布时间】:2020-05-27 14:32:41
【问题描述】:

我正在按照对话框示例从以下位置打开文件:https://github.com/electron/electron-api-demos

我从示例中复制了代码。打开文件对话框确实有效,我可以选择一个文件,但无法弄清楚为什么将文件路径发送回渲染器的箭头功能不起作用(console.log 没有记录任何内容)。

谁能发现哪里出了问题? 该项目是使用电子锻造开始的,我的操作系统是 linux。 谢谢

index.js

const { app, BrowserWindow, ipcMain, dialog, } = require('electron');
require('electron-reload')(__dirname);
const path = require('path');

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit();
}


const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  });

  // and load the index.html of the app.
  mainWindow.loadFile(path.join(__dirname, 'index.html'));

  // Open the DevTools.
  mainWindow.webContents.openDevTools();
};

// 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', () => {
  // On OS X 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', () => {
  // On OS X 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 (BrowserWindow.getAllWindows().length === 0) {
    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 import them here.


ipcMain.on('open-file-dialog', (event) => {
  dialog.showOpenDialog(
    {
      properties: ['openFile',]
    },
    (files) => {
      console.log('ok')
      if (files) {
        event.sender.send('select-file', files)
      }
    })
})

index.html

<!DOCTYPE html>
<html>

<head>
  <title>Hello</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>
  <div>
    <div>
      <button class="demo-button" id="select-directory">Select file</button>
      <span class="demo-response" id="selected-file"></span>
    </div>
    <br><br>
  </div>
  <script>
    const electron = require('electron')
    const { ipcRenderer } = electron

    const selectDirBtn = document.getElementById('select-directory')

    selectDirBtn.addEventListener('click', (event) => {
      ipcRenderer.send('open-file-dialog')
    })

    ipcRenderer.on('select-file', (event, path) => {
      console.log(path)
      document.getElementById('selected-file').innerHTML = `You selected: ${path}`
    })

  </script>
</body>

</html>

【问题讨论】:

  • Electron 6 中的对话框 API 已更改。请参阅 stackoverflow.com/questions/59698444/…
  • Electron 演示仍然使用 Electron 5。
  • 谢谢。我没想到演示会这么过时。我使用的是第 8 版!

标签: javascript electron electron-forge


【解决方案1】:

dialog API 已随着 Electron 6 的发布进行了修改。

dialog.showOpenDialog() 和其他对话框函数现在返回承诺并且不再接受回调函数。还有一些同步对应物以阻塞方式返回选择结果,例如dialog.showOpenDialogSync().

示例用法(在渲染器进程中)

const remote = require("electron").remote
const dialog = remote.dialog

dialog.showOpenDialog(remote.getCurrentWindow(), {
    properties: ["openFile", "multiSelections"]
}).then(result => {
    if (result.canceled === false) {
        console.log("Selected file paths:")
        console.log(result.filePaths)
    }
}).catch(err => {
    console.log(err)
})

截至 2020 年 2 月,electron-api-demos 使用 Electron 5。这就是为什么他们的对话调用代码仍使用旧形式的原因。

【讨论】:

    猜你喜欢
    • 2015-10-23
    • 1970-01-01
    • 2021-03-23
    • 2020-10-19
    • 2022-12-14
    • 2020-02-08
    相关资源
    最近更新 更多