Is it possible to invoke a non-static method from a static method in a Blazor application while changing a C# property value from JavaScript using DotNet.invokeMethodAsync? I have the following code working so far:
JS File:
[script.js]
function ChangeContentJS() {
DotNet.invokeMethodAsync('InvokeFromJsApp', "ChangeParaContentValue", "New Content");
}
Razor page:
[Index.razor]
@page "/"
@inject IJSRuntime JSRuntime
<h1>Change C# property value from JavaScript</h1>
<br />
<button @onclick='ButtonClickHandler'>Change Content - JS</button>
<br />
<p>@ParaContent</p>
@code {
public static string ParaContent = "Some Text Content";
public async Task ButtonClickHandler()
{
await JSRuntime.InvokeAsync<string>("ChangeContentJS");
}
[JSInvokable]
public static void ChangeParaContentValue(string value)
{
ParaContent = value;
RunNewCode(); //DOESNT WORK AS ITS A NON-STATIC METHOD
}
public void RunNewCode()
{
jsRuntime.InvokeVoidAsync("RunFunction");
}
}
I am facing issues trying to run a non-static method within a static method in a Blazor application. How can I achieve this?
When attempting to make the following method static, I encountered the error below:
public static void RunNewCode()
{
jsRuntime.InvokeVoidAsync("RunFunction");
}
CS0120: An object reference is required for the nonstatic field, method, or property 'JSRuntime'
How do I make this static: @inject IJSRuntime JSRuntime