简体   繁体   English

Blazor EventCallback with Multiple Params - 如何响应父主机控件中的事件

[英]Blazor EventCallback with Multiple Params - how to respond to event in parent host control

I have a blazor component with an EventCallBack parameter that utilized the new struct format allowing multiple arguments我有一个带有 EventCallBack 参数的 blazor 组件,它使用了允许多个 arguments 的新结构格式

[Parameter] public EventCallback<(EmployeeShiftDay, DateTime, DateTime)> NewDayScrolledIntoView { get; set; }

eventcallback is invoked in child normally as such eventcallback 通常在 child 中调用

await NewDayScrolledIntoView.InvokeAsync(p1, p2, p3);

In my host page I have the corresponding method to handle the invoke在我的主机页面中,我有相应的方法来处理调用

private void NewDayInView(EmployeeShiftDay dayInView, DateTime weekStart, DateTime weekEnd)
{
   ...
}

How do I add the markup for this EventCallBack in the host component - I need of course 3 params not just one如何在主机组件中添加此 EventCallBack 的标记 - 我当然需要 3 个参数而不仅仅是一个

<ShiftCardScroller NewDayScrolledIntoView="@((args) => NewDayInView(args))" ></ShiftCardScroller>

You are invoking it deconstructed:您正在解构调用它:

await NewDayScrolledIntoView.InvokeAsync((p1, p2, p3));

When the event is received deconstruct it then:收到事件后,将其解构:

<ShiftCardScroller NewDayScrolledIntoView="@((args)=> NewDayInView(args.Item1,args.Item2,args.Item3))" />

For me @Brian Parker's solution did not work a slightly modification needed:对我来说@Brian Parker 的解决方案不起作用,需要稍作修改:

(same) (相同的)

await NewDayScrolledIntoView.InvokeAsync((p1, p2, p3));

Tuple casting:元组铸造:

<ShiftCardScroller NewDayScrolledIntoView="@((args)=> NewDayInView(((EmployeeShiftDay,DateTime,DateTime))args))" />

Method takes one argument.方法接受一个参数。 The type is consistent with the previously casted type:该类型与之前转换的类型一致:

private void NewDayInView((EmployeeShiftDay,DateTime,DateTime)args)
{
   ...
}

you should pass only the name of the Method to the child componant您应该只将方法的名称传递给子组件

<ShiftCardScroller NewDayScrolledIntoView="@NewDayInView" ></ShiftCardScroller>

and in the child componant you could invoke the callback with the the parameters that you want在子组件中,您可以使用所需的参数调用回调

NewDayScrolledIntoView.invokeAsync(Param1,Param1,Param3)

You can use custom Args Type as below:您可以使用自定义 Args 类型,如下所示:

public class EventCallbackArgs
{
    public bool Confirm { get; set; }
    public Guid Id { get; set; }
    public string Name { get; set; }
}

This is a sample blazor code这是一个示例 blazor 代码

<button type="button" class="btn btn-danger" @onclick="() => OnConfirmationChange(true)">
    Delete
</button>

On Delete button click, OnConfirmationChange is called, inside the code of OnConfirmationChange, you can prepare EventCallbackArgs and invoke ConfirmationChanged单击 Delete 按钮时,会调用OnConfirmationChange ,在 OnConfirmationChange 的代码中,您可以准备EventCallbackArgs并调用ConfirmationChanged

[Parameter]
public EventCallback<EventCallbackArgs> ConfirmationChanged { get; set; }
public EventCallbackArgs args { get; set; }

protected async Task OnConfirmationChange(bool deleteCofirmed)
{
    ShowConfirmation = false;
    args = new EventCallbackArgs { Id = id, Name = name };
    args.Confirm = deleteCofirmed;
    await ConfirmationChanged.InvokeAsync(args);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM