[英]Blazor EventCallback parameter not firing
I am trying to get a child component to update a list on the parent.我正在尝试让子组件更新父组件的列表。 To do this I setup an EventCallback that takes a list and sends it to the parent.
为此,我设置了一个 EventCallback,它接受一个列表并将其发送给父级。 The issue is the event never fires and the HasDelegate variable on the callback is false.
问题是事件永远不会触发并且回调中的 HasDelegate 变量为 false。
Parent .razor.cs:
public async Task UpdateSelectedCompanies(List<CompanyStub> companies)
{
_selectedCompanies = companies;
await InvokeAsync(StateHasChanged);
}
Parent .razor:
<CompanyTable IncludeCheckbox="true" UpdateCompanies="@UpdateSelectedCompanies"></CompanyTable>
Child .razor.cs:
[Parameter] public EventCallback<List<CompanyStub>> UpdateCompanies { get; set; }
private async Task CheckboxRowSelectHandler(RowSelectEventArgs<CompanyStub> args)
{
SelectedCompanies.Add(args.Data);
await UpdateCompanies.InvokeAsync(SelectedCompanies);
}
CheckboxRowSelectHandler does get called, but the UpdateCompanies event never fires. CheckboxRowSelectHandler 确实被调用,但 UpdateCompanies 事件永远不会触发。
I am expecting for the event to be fired, but the event never gets fired.我期待事件被触发,但事件永远不会被触发。
Here's a simplified version of your code that works.这是您的代码的简化版本。 Use it to test your child/parent.
用它来测试你的孩子/父母。
One thing to check: Is your razor.cs file correctly linked to the razor component file?要检查的一件事:您的 razor.cs 文件是否正确链接到 razor 组件文件?
ChildComponent.razor ChildComponent.razor
<div class="m-2 p-2 bg-light">
<h3>ChildComponent</h3>
<button class="btn btn-primary" @onclick=this.OnClick>Click me</button>
</div>
@code {
[Parameter] public EventCallback<string> ValueChanged { get; set; }
private async Task OnClick()
=> await this.ValueChanged.InvokeAsync($"Set at {DateTime.Now.ToLongTimeString()}");
}
@page "/"
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<ChildComponent ValueChanged="@this.OnClicked" />
@if (!string.IsNullOrWhiteSpace(this.message))
{
<div class="alert alert-info">
@message
</div>
}
@code {
private string message = string.Empty;
// No call to StateHasChanged required
// The ComponentBase UI handler does it
private void OnClicked(string value)
=> message = value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.