In a razor file, I inject @inject IJSRuntime JSRuntime
. In the code behind, I invoke a JS function with the following code:
private async Task DownloadPdf(bool foo)
{
await using var module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./create-pdf.js");
await module.InvokeVoidAsync("generatePdf", foo);
}
This works well but now I need to call a JS function from C# code that is not code behind but a separate *.cs file:
public static class Bar
{
public static double Baz()
{
// do something
// call JS function
return baz;
}
}
I tried this approach but JSRuntime
is always null
.
How can I invoke JS from my static C# method?