如何对发出的事件使用异步 event-handler?

How to use asynchronous event-handler for emitted event?

我有一个 parent 和一个 child component.In child 我向 parent.However 发出一个事件 我想要这个 Action成为 async,因为我希望 parent 在收到 event.How 时执行 async 操作,我可以这样做吗?

Child

@functions{
[Parameter] protected Action onclose { get; set; }
[Parameter] protected  Action<bool> onsubmit { get; set; } //i want this handler to be async in the parent

string campaign; 
public async Task OnSubmitAsync() {
   var created = await this.service.CreateCampaignAsync(parameters);
        Console.WriteLine("Result of creation:" + created.ToString());
        this.onsubmit?.Invoke(created);
    }

Parent

 <CampaignForm onclose="@(()=>OnModalClosed())" onsubmit="@(async(x)=>OnSubmit(x))"></CampaignForm>
    @functions{
       public async Task OnSubmit(bool value) {
                //do some awaiting here 
       }
       public void OnModalClose()=>....; //do something sync ;
    }

在子组件上执行此操作:

@functions{
    // Define a property to store the Action delegate 
    [Parameter] protected  Action<bool> onsubmit { get; set; } 

    // More code here...

    public async Task OnSubmitAsync() {
   var created = await this.service.CreateCampaignAsync(parameters);
        Console.WriteLine("Result of creation:" + created.ToString());
        // Call back the parent's method
        onsubmit?.Invoke(created);
    }

 }

在父组件上执行此操作: 请注意,您应该将 OnSubmit 方法的标识符分配给 func 委托属性 onsubmit (onsubmit="OnSubmit")

 <CampaignForm onclose="OnModalClosed" onsubmit="OnSubmit"></CampaignForm>
    @functions{
       public async void OnSubmit(bool value) {
                //do some awaiting here 
       }
       public void OnModalClose()=>....; //do something sync ;
    }

希望这对您有所帮助... 如果对您有帮助,请将我的回答标记为已接受希望这对您有所帮助...