我想收到带有多个参数的ipcRenderer.send消息。 JavaScript看起来像这样:
document.getElementById("btn-submit").addEventListener("click", () => {
ipcRenderer.send("btn-submit", [document.getElementById("uid").nodeValue, document.getElementById("pw").nodeValue]);
});
当我尝试创建位于 Controller 内的监听功能时,当我将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方法传递的多个参数?
最佳答案
尽管.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);
});