在 Blazor (异步任务初始化异步) 生命周期中使用两个等待无法正常工作



我在操作中使用以下方法。

protected override async Task OnInitializedAsync()
{
forecasts = await Http.GetJsonAsync<WeatherForecast[]>("sample-data/weather.json");
await someOperation(); // this action is not perfomred
}

对此查询的任何建议

我过去遇到过这个问题,我能够像这样修复它:

protected override async Task OnInitializedAsync()
{
Http.GetJsonAsync<WeatherForecast[]>("sample-data/weather.json").ContinueWith(task => forecasts = task.Result); //Creates a continuation that executes asynchronously when the target Task completes.

await someOperation(); // his action can also be done using ContinueWith if needed
}

继续阅读文档:

希望这有帮助。

最新更新