您需要在RederFragment组件到父方法的事件回调方面提供帮助的以下代码段
GenericList组件
@typeparam TItem
@foreach (var item in Items)
{
@ChildContent(item)
}
@code {
[Parameter] public List<TItem> Items { get; set; }
[Parameter] public RenderFragment<TItem> ChildContent { get; set; }
void DeleteItem(TItem item)
{
Items.Remove(item);
InvokeAsync(StateHasChanged);
}
}
电话组件
<InputText @bind-Value="NewPhone.PhoneNumber"></InputText>
@code {
public Phone NewPhone { get; set; }
[Parameter] public EventCallback<Phone> DeletePhone { get; set; }
public class Phone
{
public int Id { get; set; }
public string PhoneNumber { get; set; }
}
}
现在当我使用GenericList组件时,如何将EventCallback DeletePhone传递给GenericList上的Delete方法
<GenericListComponent Items="PhoneList" TItem=Phone>
<ChildContent Context="phone">
<PhoneComponent Phone="phone" DeletePhone=**"?"**></PhoneComponent>
</ChildContent>
</GenericListComponent>