当前线程未与 Dispatcher 关联。在触发渲染时使用 InvokeAsync() 将执行切换到 Dispatcher
The current thread is not associated with the Dispatcher. Use InvokeAsync() to switch execution to the Dispatcher when triggering rendering
我正在使用 Dotnet Core Blazor,在删除事件后使用 EventCallBack 绑定父网格时出现以下错误。下面是使用子组件的代码
<tbody>
@foreach (var employee in Employees)
{
<BlazorAppDemo.Pages.Controls.EmployeeList Employee="employee"
ShowFooter="ShowFooter"
OnEmployeeSelectionChange="onEmployeeSelectionChanged"
btnDeleteClick="OnEmployeeDeleted" >
</BlazorAppDemo.Pages.Controls.EmployeeList>
}
</tbody>
EmployeeList 子组件在下方
<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");
}
}
父页面已订阅删除事件:
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 倍)。
当您在 Blazor 中正确使用 async/await 时,您几乎不需要它。
当您使用它时,您要求事件的其余部分 运行 在不同的线程上进行。大多数情况下是错误的线程。
我正在使用 Dotnet Core Blazor,在删除事件后使用 EventCallBack 绑定父网格时出现以下错误。下面是使用子组件的代码
<tbody>
@foreach (var employee in Employees)
{
<BlazorAppDemo.Pages.Controls.EmployeeList Employee="employee"
ShowFooter="ShowFooter"
OnEmployeeSelectionChange="onEmployeeSelectionChanged"
btnDeleteClick="OnEmployeeDeleted" >
</BlazorAppDemo.Pages.Controls.EmployeeList>
}
</tbody>
EmployeeList 子组件在下方
<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");
}
}
父页面已订阅删除事件:
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 倍)。
当您在 Blazor 中正确使用 async/await 时,您几乎不需要它。
当您使用它时,您要求事件的其余部分 运行 在不同的线程上进行。大多数情况下是错误的线程。