【问题标题】:How to receive multiple arguments in Electron.net如何在 Electron.net 中接收多个参数
【发布时间】:2023-03-25 08:55:02
【问题描述】:

我想接收带有多个参数的 ipcRenderer.send 消息。 JavaScript 看起来像这样:

document.getElementById("btn-submit").addEventListener("click", () => {
                ipcRenderer.send("btn-submit", [document.getElementById("uid").nodeValue, document.getElementById("pw").nodeValue]);
            });

当我尝试创建位于控制器内的监听函数时,当我将 args 引用为数组时收到语法错误,如下所示:

Electron.IpcMain.On("btn-submit", async (args) =>
                {
                    MessageBoxOptions options = new MessageBoxOptions(String.Format("UID: {0} PW:{1}",args[0],args[1]))
                    {
                        Type = MessageBoxType.info,
                        Title = "Information",
                        Buttons = new string[] { "Yes", "No" }
                    };

                    var result = await Electron.Dialog.ShowMessageBoxAsync(options);
                });

如何在ipcMain监听方法中接收ipcRenderer.send方法传递的多个参数?

【问题讨论】:

    标签: javascript c# electron electron.net


    【解决方案1】:

    虽然.On(...) 采用Action<object>,但当有多个参数时,您可以将object 强制转换为List<object>,这样可以解决您的语法问题:

    Electron.IpcMain.On("btn-submit", async (args) =>
                    {
                        var listArgs = (List<object>)args;
                        MessageBoxOptions options = new MessageBoxOptions(String.Format("UID: {0} PW:{1}",listArgs[0],listArgs[1]))
                        {
                            Type = MessageBoxType.info,
                            Title = "Information",
                            Buttons = new string[] { "Yes", "No" }
                        };
    
                        var result = await Electron.Dialog.ShowMessageBoxAsync(options);
                    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      相关资源
      最近更新 更多