我创建了一个简单的 Blazor MAUI 项目。\n我添加了一个简单的 Razor 类库,其中包含两个 JavaScript 文件、一个带有三个按钮的简单 razor 组件以及两个用于 JavaScript 方法调用的 C# 包装类。\n其中一个 JavaScript 文件使用导出\n第一个包装类使用生成的默认方法代码
\n{\nvar module = await moduleTask.Value;\nreturn await module.InvokeAsync<string>(\xe2\x80\x9cshowPrompt\xe2\x80\x9d, message);\n}\nRun Code Online (Sandbox Code Playgroud)\n第二个包装器使用 JSRuntime JS
\n{\nreturn await JS.InvokeAsync<string>(\xe2\x80\x9cshowPrompt\xe2\x80\x9d, message);\n}\nRun Code Online (Sandbox Code Playgroud)\n我的基本 Razor 组件定义如下:
\n@inject ExampleJsInterop exampleJsWithModule\n@inject IJSRuntime js\n@inject ExampleJsInteropWithJSRunTime exampleJsRunTime\n<div class=\xe2\x80\x9cmy-component\xe2\x80\x9d>\nThis component is defined in the <strong>RazorClassLibrary</strong> library.\n<button class=\xe2\x80\x9cbtn btn-primary\xe2\x80\x9d @onclick=\xe2\x80\x9cCallJSFunctionFromCSWithModule\xe2\x80\x9d>Call JS function from C# with Module</button>\n<button class=\xe2\x80\x9cbtn btn-primary\xe2\x80\x9d @onclick=\xe2\x80\x9cCallJSFunctionFromCSWithJSruntime\xe2\x80\x9d>Call JS function from C# With JSruntime</button>\n<button class=\xe2\x80\x9cbtn btn-primary\xe2\x80\x9d @onclick=\xe2\x80\x9cCallJSFunctionFromScript\xe2\x80\x9d>Call JS function from script</button>\n</div>\n@code{\nprivate async …Run Code Online (Sandbox Code Playgroud)