blazor 文档建议在从 JavaScript 调用 .NET 组件实例时使用辅助类。
辅助类什么也不做,只是委托给组件方法:
public class MessageUpdateInvokeHelper
{
private Action action;
public MessageUpdateInvokeHelper(Action action)
{
this.action = action;
}
[JSInvokable("BlazorSample")]
public void UpdateMessageCaller()
{
action.Invoke();
}
}
Run Code Online (Sandbox Code Playgroud)
@inject IJSRuntime JS
@code {
private string message = "Select one of these list item buttons.";
private MessageUpdateInvokeHelper? messageUpdateInvokeHelper;
protected override void OnInitialized()
{
messageUpdateInvokeHelper = new MessageUpdateInvokeHelper(UpdateMessage);
}
private void UpdateMessage()
{
message = "UpdateMessage Called!";
StateHasChanged();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不在组件实例上定义 [JSInvokable] 呢?
@code {
private string message = "Select one of these list item buttons.";
[JSInvokable("BlazorSample")]
private void UpdateMessage()
{
message = "UpdateMessage Called!";
StateHasChanged();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
189 次 |
最近记录: |