file - 在 Electron 版本中,当从dialog.showSaveDialog调用时,BrowserWindow.getFocusedWindow返回null。

标签 file dialog focus electron

在 Electron 中,有没有一种方法可以在调用dialog.showSaveDialog的过程中获得当前聚焦的窗口?从dialog.showOpenDialog内可以正常工作,但从null内完成后返回dialog.showSaveDialog

这有效:

  dialog.showOpenDialog(function (filepaths) {
    if(filepaths !== undefined){
      BrowserWindow.getFocusedWindow().send('file-open', filepaths[0])
    }
  });

这会抛出TypeError: Cannot read property 'send' of null:
dialog.showSaveDialog(options, function (filepath) {
  if (filepath !== undefined) {
    BrowserWindow.getFocusedWindow().send('file-save', filepath)
  }
}); 

最佳答案

通过将代码更改为以下内容来解决此问题:

const activeWindow = BrowserWindow.getFocusedWindow()

dialog.showSaveDialog(options, function (filepath) {
  if (filepath !== undefined) {
    activeWindow.send('file-save', filepath)
  }
});

仍然对为什么有人有一个为什么它不起作用的答案仍然很感兴趣。

关于file - 在 Electron 版本中,当从dialog.showSaveDialog调用时,BrowserWindow.getFocusedWindow返回null。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40638518/

相关文章:

android - 在 Android 中以编程方式选择 EditText

java - 缓存文件服务器

java - 为什么我的动画会剪切消息框?

css - 带焦点的菜单添加文本

android - 在 GCM onMessage() 中显示对话框

c# - 如何从 MDI 子窗体(MDI 应用程序)打开窗体对话框

java - 将焦点从一个 EditText 移到另一个

java - 在 jdk 6 与 jdk 8 中创建临时目录

python - IF 语句不比较从文件接收的值

java - 序列化包含 BufferedImages 的对象