DRS*_*J99 6 blazor-webassembly maui-blazor
我创建了一个简单的 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 void CallJSFunctionFromCSWithModule()\n{\n await exampleJsWithModule.Prompt("Hello, this does not work!");\n}\nprivate async void CallJSFunctionFromCSWithJSruntime()\n{\n await exampleJsRunTime.Prompt("Hello, this does not work!");\n}\nprivate async void CallJSFunctionFromScript()\n{\n await js.InvokeAsync<string>("showPromptFromJS", "Hello, this work!");\n}\n}\nRun Code Online (Sandbox Code Playgroud)\n只有 CallJSFunctionFromScript 有效。\nCallJSFunctionFromCSWithJSruntime 和 CallJSFunctionFromCSWithModule 失败。\n我已在 MauiProgram.cs 中调用:
\nbuilder.Services.AddSingleton<ExampleJsInterop>();\nbuilder.Services.AddSingleton<ExampleJsInteropWithJSRunTime>();\nRun Code Online (Sandbox Code Playgroud)\n我在index.html中也有:
\n<script src=\xe2\x80\x9c./_content/RazorClassLibrary/exampleJsInterop.js\xe2\x80\x9d></script>\n<script src=\xe2\x80\x9c./_content/RazorClassLibrary/JsInterop.js\xe2\x80\x9d></script>\nRun Code Online (Sandbox Code Playgroud)\n但是,如果我创建一个 blazor WebAssembly 应用程序而不是 Blazor Maui 应用程序\nCallJSFunctionFromScript 和 CallJSFunctionFromCSWithJSruntime 可以工作,但不能 CallJSFunctionFromCSWithModule 。\n为什么 CallJSFunctionFromCSWithJSruntime 和 CallJSFunctionFromCSWithModule 在 Blazor Maui 中失败?\n以及为什么 CallJSFunctionFromCSWithJSruntime 在 Blazor WebAssembly 应用程序中工作而不是在 Blazor 中工作毛伊岛?\n错误我得到的是:
\nSystem.NullReferenceException\nHResult = 0x80004003\nMessage=Object reference not set to an instance of an object.\nSource=Microsoft.AspNetCore.Components.WebView\nStackTrace:\nat Microsoft.AspNetCore.Components.WebView.Services.WebViewJSRuntime.BeginInvokeJS(Int64 taskId, String identifier, String argsJson, JSCallResultType resultType, Int64 targetInstanceId)\nat Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, CancellationToken cancellationToken, Object[] args)\nat Microsoft.JSInterop.JSRuntime.<InvokeAsync>d__161.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Threading.Tasks.ValueTask1.get_Result()\nat System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()\nat RazorClassLibrary.ExampleJsInteropWithJSRunTime.<Prompt>d__2.MoveNext() in C:\\Project\\MauiBlazorApp2\\RazorClassLibrary\\ExampleJsInterop.cs:line 24\nThis exception was originally thrown at this call stack:\n[External Code]\nRazorClassLibrary.ExampleJsInteropWithJSRunTime.Prompt(string) in ExampleJsInterop.cs\nRun Code Online (Sandbox Code Playgroud)\n谢谢
\n更新:\n显然,如果我使用\nbuilder.Services.AddScoped();\而不是\nbuilder.Services.AddSingleton();\nCallJSFunctionFromCSWithJSruntime 有效。\n为什么 Blazor 应用程序可与 Singleton 配合使用,而 Maui Blaxor 需要 Scoped?\n还有使用默认生成的代码调用 javascript,使用 module+import js 文件会失败,无论是作用域还是单例?
\n| 归档时间: |
|
| 查看次数: |
1948 次 |
| 最近记录: |