Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor WebAssembly in process runtime #128

Open
wazazhang opened this issue Jul 28, 2022 · 0 comments
Open

Blazor WebAssembly in process runtime #128

wazazhang opened this issue Jul 28, 2022 · 0 comments

Comments

@wazazhang
Copy link

从 .NET 调用 JavaScript
本部分仅适用于 Blazor WebAssembly应用。

默认情况下,JS 互操作调用是异步的,无论调用的代码是同步还是异步。 默认情况下,调用是异步的,以确保组件在 Blazor 托管模型 Blazor Server 和 Blazor WebAssembly 之间兼容。 在 Blazor Server 上,所有 JS 互操作调用必须是异步的,因为它们是通过网络连接发送的。

如果你确定你的应用只在 Blazor WebAssembly 上运行,则可以选择执行同步 JS 互操作调用。 这比进行异步调用的开销略少,并且可能会导致呈现周期更少,因为在等待结果时没有中间状态。

若要在 Blazor WebAssembly 应用中进行从 .NET 到 JavaScript 的同步调用,请将 IJSRuntime 强制转换为 IJSInProcessRuntime 以进行 JS 互操作调用:

protected T CallMethod(string method)
{
var js = (IJSInProcessRuntime)_jsRuntime;
return js.Invoke($"{NAMESPACE_PREFIX}.{_contextName}.{CALL_METHOD_ACTION}", Canvas, method);
//return CallMethodAsync(method).GetAwaiter().GetResult();
}
protected T CallMethod(string method, params object[] value)
{
var js = (IJSInProcessRuntime)_jsRuntime;
return js.Invoke($"{NAMESPACE_PREFIX}.{_contextName}.{CALL_METHOD_ACTION}", Canvas, method, value);
//return CallMethodAsync(method, value).GetAwaiter().GetResult();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant