使用 wasmbrowser 项目和 ASP.NET Core 8,我可以通过 JSImport 互操作从 Javascript 调用 .NET
Microsoft 文档包含此示例,但没有任何参数。
C#
[JSImport("window.location.href", "main.js")]
internal static partial string GetHRef();
JavaScript
setModuleImports("main.js", {
window: {
location: {
href: () => globalThis.window.location.href
}
}
});`
如果我有参数,我该如何做同样的事情?例如,如果我在 C# 端有这个函数,我该如何编写 setModuleImports
[JSImport("window.location.test", "main.js")]
internal static partial string Test(string a, int b);
我正在 wasmbrowser NET8 项目中尝试这个。在文档中WASM上的JavaScript互操作看起来是可能的,但我还没有找到方法
我想通了,你应该这样做
C#
[JSImport("ping4", "main.js")]
internal static partial string Ping4(string a, int b);
js
setModuleImports("main.js", {
ping4: (a, b) => { return a + ";" + b; }
});
用法
Ping4("hi", 7) returns "hi;7"