在电子中,BrowserWindow.getFocusedWindow在从dialog.showSaveDialog调用时返回null

时间:2016-11-16 17:28:16

标签: file dialog focus 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)
  }
}); 

1 个答案:

答案 0 :(得分:1)

通过将代码更改为此来管理以解决此问题:

const activeWindow = BrowserWindow.getFocusedWindow()

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

仍然有兴趣回答为什么如果有人有,否则它不会起作用。