[英]Blazor EventCallback parameter not firing
我正在嘗試讓子組件更新父組件的列表。 為此,我設置了一個 EventCallback,它接受一個列表並將其發送給父級。 問題是事件永遠不會觸發並且回調中的 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 確實被調用,但 UpdateCompanies 事件永遠不會觸發。
我期待事件被觸發,但事件永遠不會被觸發。
這是您的代碼的簡化版本。 用它來測試你的孩子/父母。
要檢查的一件事:您的 razor.cs 文件是否正確鏈接到 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.