javascript - dialog.showMessageBox 不返回 Electron main.js 中的按钮索引

标签 javascript callback dialog electron messagebox

我有一个消息框,当用户在 dashboardWindow 上单击关闭(Windows 操作系统右上角的 X 按钮)时,该消息框将打开

dashboardWindow.on("close", (event) => {
    event.preventDefault();
    console.log("before message box");
    dialog.showMessageBox(
      dashboardWindows,
      {
        message: "Test",
        buttons: ["Default Button", "Cancel Button"],
        defaultId: 0, // bound to buttons array
        cancelId: 1 // bound to buttons array
      },
      (response) => {
        if (response === 0) {
          // bound to buttons array
          console.log("Default button clicked.");
        } else if (response === 1) {
          // bound to buttons array
          console.log("Cancel button clicked.");
        }
      }
    );
    console.log("after message box");
  });
}

当我关闭 dashboardWindow 时消息框打开,但我无法使 response === 0 工作。 Samehow console.log("after message box"); 即使没有点击按钮也已经运行。我如何使响应起作用(消息框上的返回索引按钮)?

log on window close

最佳答案

关于dialog.showMessageBox请引用最新的API文档:此方法返回一个 Promise 对象,并且不再像以前那样使用回调函数,直到 Electron v5.x.x。

Returns Promise<Object> - resolves with a promise containing the following properties:

  • response Number - The index of the clicked button.
  • checkboxChecked Boolean - The checked state of the checkbox if checkboxLabel was set. Otherwise false.

这应该可以工作(尽管在您的上下文中未经测试):

dashboardWindow.on("close", (event) => {
    event.preventDefault();
    console.log("before message box");
    dialog.showMessageBox(
      dashboardWindows,
      {
        message: "Test",
        buttons: ["Default Button", "Cancel Button"],
        defaultId: 0, // bound to buttons array
        cancelId: 1 // bound to buttons array
      })
      .then(result => {
        if (result.response === 0) {
          // bound to buttons array
          console.log("Default button clicked.");
        } else if (result.response === 1) {
          // bound to buttons array
          console.log("Cancel button clicked.");
        }
      }
    );
    console.log("after message box");
  });

关于javascript - dialog.showMessageBox 不返回 Electron main.js 中的按钮索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59349045/

相关文章:

javascript - Phaser 是否渲染可见设置为 false 的 Sprite ?

javascript - typescript 可选扩展接口(interface)

javascript - 将对象作为参数发送到回调函数-nodejs

javascript - Jquery Bootstrap Modal...如何从另一个模式对话框中打开不同的模式对话框

javascript - jqPlot图形缩放后向右移动

javascript - 使用 jQuery 获取未附加元素的 CSS

javascript - 存储完成操作后要执行的任意操作

asynchronous - 如何使用 async.queue 避免 "Callback was already called"?

android - 如何删除显示为对话框的 Activity 标题

reactjs - 单击菜单项后,Material UI 菜单未关闭