如何使用异步事件处理程序来发出事件?

时间:2019-01-25 15:10:45

标签: c# asynchronous eventemitter blazor

我有一个父级和一个子级组件。在子级中,我向父级发出一个事件。但是,我希望此Actionasync,因为我希望父级执行{{1 }}他收到事件时的操作。我该怎么办?

孩子

async

父母

@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);
    }

1 个答案:

答案 0 :(得分:1)

在子组件上执行此操作:

@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 ;
    }

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