Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Blazor WAsm-Can';t调用返回值的JS函数_C#_.net Core_Blazor - Fatal编程技术网

C# Blazor WAsm-Can';t调用返回值的JS函数

C# Blazor WAsm-Can';t调用返回值的JS函数,c#,.net-core,blazor,C#,.net Core,Blazor,我想做什么 我想调用一个返回值的JS函数。我完全按照文件记录的那样做了 设置 C# Log.Warning(“testest”); 等待JsRuntime.InvokeAsync(“UtilityTest.testFunc”); 日志警告(“测试”); JS var UtilityTest=新函数(){ this.testFunc=函数(){ log(“[JS-TEST]testFunc”); 返回true; }; }; 日志 \Client\Test.cs:27指的是JsRuntime.I

我想做什么
我想调用一个返回值的JS函数。我完全按照文件记录的那样做了

设置
C#

Log.Warning(“testest”);
等待JsRuntime.InvokeAsync(“UtilityTest.testFunc”);
日志警告(“测试”);
JS

var UtilityTest=新函数(){
this.testFunc=函数(){
log(“[JS-TEST]testFunc”);
返回true;
};
};
日志


\Client\Test.cs:27
指的是
JsRuntime.InvokeAsync
行。我真的不知道为什么会抛出异常(
JsRuntime.InvokeVoidAsync
works)。我希望有人能帮我解决这个问题。

这段代码适合我


@第“/”页
@注入IJSRuntime JsRuntime
试验
@结果
@代码
{
私有布尔结果{get;set;}=false;
专用异步任务测试()
{
结果=等待JsRuntime.InvokeAsync(“UtilityTest.testFunc”);
}
}
如果要在组件渲染后调用函数,则需要调用OnAfterRenderAsync

AfterRenderAsync(bool firstRender)上的受保护重写异步任务
{
if(firstRender)
{
等待JsRuntime.InvokeAsync(“UtilityTest.testFunc”);
}
等待base.OnAfterRenderAsync(firstRender);
}
您可以查看该源代码以了解Blazor的更多Javascript

TESTESTESTTESTESTESTTESTESTEST
[JS-TEST] testFunc
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: An exception occurred executing JS interop: The JSON value could not be converted to System.Boolean. Path: $ | LineNumber: 0 | BytePositionInLine: 4.. See InnerException for more details.
Microsoft.JSInterop.JSException: An exception occurred executing JS interop: The JSON value could not be converted to System.Boolean. Path: $ | LineNumber: 0 | BytePositionInLine: 4.. See InnerException for more details. ---> System.Text.Json.JsonException: The JSON value could not be converted to System.Boolean. Path: $ | LineNumber: 0 | BytePositionInLine: 4. ---> System.InvalidOperationException: Cannot get the value of a token type 'Null' as a boolean.
  at System.Text.Json.Utf8JsonReader.GetBoolean () <0x2c89c00 + 0x00090> in <filename unknown>:0 
  at System.Text.Json.Serialization.Converters.JsonConverterBoolean.Read (System.Text.Json.Utf8JsonReader& reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) <0x30ab220 + 0x00004> in <filename unknown>:0 
  at System.Text.Json.JsonPropertyInfoNotNullable`4[TClass,TDeclaredProperty,TRuntimeProperty,TConverter].OnRead (System.Text.Json.ReadStack& state, System.Text.Json.Utf8JsonReader& reader) <0x30a45a8 + 0x0003c> in <filename unknown>:0 
  at System.Text.Json.JsonPropertyInfo.Read (System.Text.Json.JsonTokenType tokenType, System.Text.Json.ReadStack& state, System.Text.Json.Utf8JsonReader& reader) <0x30a41d8 + 0x00088> in <filename unknown>:0 
  at System.Text.Json.JsonSerializer.HandleNull (System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& state) <0x30a3b58 + 0x00100> in <filename unknown>:0 
  at System.Text.Json.JsonSerializer.ReadCore (System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& readStack) <0x30a3218 + 0x0026a> in <filename unknown>:0 
   --- End of inner exception stack trace ---
  at System.Text.Json.ThrowHelper.ReThrowWithPath (System.Text.Json.ReadStack& readStack, System.Text.Json.Utf8JsonReader& reader, System.Exception ex) <0x30e4740 + 0x00028> in <filename unknown>:0 
  at System.Text.Json.JsonSerializer.ReadCore (System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& readStack) <0x30a3218 + 0x00320> in <filename unknown>:0 
  at System.Text.Json.JsonSerializer.ReadValueCore (System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& readStack) <0x309fe18 + 0x00592> in <filename unknown>:0 
  at System.Text.Json.JsonSerializer.ReadValueCore (System.Text.Json.Utf8JsonReader& reader, System.Type returnType, System.Text.Json.JsonSerializerOptions options) <0x309e998 + 0x0003e> in <filename unknown>:0 
  at System.Text.Json.JsonSerializer.Deserialize (System.Text.Json.Utf8JsonReader& reader, System.Type returnType, System.Text.Json.JsonSerializerOptions options) <0x309e830 + 0x00024> in <filename unknown>:0 
  at Microsoft.JSInterop.JSRuntime.EndInvokeJS (System.Int64 taskId, System.Boolean succeeded, System.Text.Json.Utf8JsonReader& jsonReader) <0x2c8a120 + 0x00052> in <filename unknown>:0 
   --- End of inner exception stack trace ---
  at System.Threading.Tasks.ValueTask`1[TResult].get_Result () <0x30f2538 + 0x00034> in <filename unknown>:0 
  at TestApp.Client.KeyboardManagerTest.Initialize () [0x0003d] in <Path>\Client\Test.cs:27 
  at TestApp.Client.Pages.TestPage.OnInitializedAsync () [0x00109] in <Path>\Client\Pages\TestPage.razor:49 
  at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x2ddec48 + 0x0013a> in <filename unknown>:0 
  at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x2f2fa10 + 0x000b6> in <filename unknown>:0