当前线程与分派器不关联.触发渲染时,使用InvokeAsync()将执行切换到分派器 [英] The current thread is not associated with the Dispatcher. Use InvokeAsync() to switch execution to the Dispatcher when triggering rendering
问题描述
我正在使用Dotnet Core Blazor,并在删除事件后使用EventCallBack绑定父网格时遇到以下错误.在使用子组件的代码下方
I am working on Dotnet Core Blazor and getting below error while using EventCallBack to bind the parent grid after delete events. Below the code for using Child component
<tbody>
@foreach (var employee in Employees)
{
<BlazorAppDemo.Pages.Controls.EmployeeList Employee="employee"
ShowFooter="ShowFooter"
OnEmployeeSelectionChange="onEmployeeSelectionChanged"
btnDeleteClick="OnEmployeeDeleted" >
</BlazorAppDemo.Pages.Controls.EmployeeList>
}
</tbody>
EmployeeList子组件在下面
EmployeeList child component is below
<tr>
@*<td>@Employee.EmployeeId</td>*@
<td><input type="checkbox" @onchange="CheckBoxChanged" /></td>
<td>@Employee.Department.DepartmentName</td>
<td>@Employee.FirstName</td>
<td>@Employee.DateOfBirth.ToShortDateString()</td>
<td><img class="card-img-top img-thumb" style="width:50px;height:50px" src="@Employee.PhotoPath" /></td>
@if (ShowFooter)
{
<AuthorizeView Roles="Admin" Policy="OldEmployeeBefore2020">
<Authorized>
<td>
<a href="@($"employeedetails/{Employee.EmployeeId}")" class="btn btn-primary m-1">View</a>
<a href="@($"employeeEdit/{Employee.EmployeeId}")" class="btn btn-primary m-1">Edit</a>
@*<a href="#" class="btn btn-danger m-1">Delete</a>*@
<button class="btn btn-danger m-1" @onclick="btnDelete_Click">
</button>
</td>
</Authorized>
</AuthorizeView>
}
删除按钮事件:
[Parameter]
public EventCallback<int> btnDeleteClick { get; set; }
protected async Task btnDelete_Click()
{
var response = await EmployeeService.Delete(Employee.EmployeeId).ConfigureAwait(false);
if (string.IsNullOrEmpty(response.ErrorMessage))
{
await btnDeleteClick.InvokeAsync(Employee.EmployeeId);
//NavigationManager.NavigateTo("employee");
}
}
父页面已订阅了删除事件:
Parent page has subscribed the delete event:
protected async Task OnEmployeeDeleted(int id)
{
var response = await EmployeeService.GetEmployees().ConfigureAwait(false);
if (response.Result != null)
{
Employees = response.Result.Result;
}
else
{
PageMessage = response.ErrorMessage;
}
}
推荐答案
您应该删除.ConfigureAwait(false)
(据我所知,为2倍).
You should remove the .ConfigureAwait(false)
(2x as far as I coud see).
在Blazor中正确使用async/await时,几乎应该永远不需要它.
When you use async/await correctly in Blazor you should almost never need it.
当您使用它时,您要求事件的其余部分在另一个线程上运行.在大多数情况下,线程错误.
When you do use it you are asking for the remainder of the event to be run on a different thread. The wrong thread in most cases.
这篇关于当前线程与分派器不关联.触发渲染时,使用InvokeAsync()将执行切换到分派器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!