【发布时间】:2021-02-26 12:27:09
【问题描述】:
我知道我们可以通过调用 DotNet.invokeMethodAsync 方法从 JavaScript 更改 Blazor 中的 C# 属性值。我有以下工作,但在这个方法中我还想调用一个非静态方法。
JS 文件:
[script.js]
function ChangeContentJS() {
DotNet.invokeMethodAsync('InvokeFromJsApp', "ChangeParaContentValue", "New Content");
}
剃刀页面:
[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");
}
}
我正在尝试在静态方法中运行非静态方法(在 BLAZOR 应用程序中)。这个方法怎么调用?
我尝试将以下方法设为静态并得到以下错误:
public static void RunNewCode()
{
jsRuntime.InvokeVoidAsync("RunFunction");
}
CS0120:非静态字段需要对象引用, 方法或属性“JSRuntime”
如何使这个静态化:@inject IJSRuntime JSRuntime
【问题讨论】:
标签: javascript c# .net blazor