服务器端 Razor 允许您描述的内容。该视频介绍了此 Github存储库中的代码,该代码展示了如何通过修改ForecastService
服务器端 Blazor 模板中的示例来使用 IAsyncEnumerable。
修改服务本身很容易,实际上会产生更简洁的代码:
public async IAsyncEnumerable<WeatherForecast> GetForecastAsync(DateTime startDate)
{
var rng = new Random();
for(int i=0;i<5;i++)
{
await Task.Delay(200);
yield return new WeatherForecast
{
Date = startDate.AddDays(i),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
};
}
}
另一方面,Blazor 页面更复杂。不仅循环必须在 HTML 显示之前完成,您不能在页面本身中使用await foreach
,因为它不是异步的。您只能在代码块中定义异步方法。
您可以做的是枚举 IAsyncEnumerable 并通知页面在每次更改后自行刷新。
渲染代码本身不需要更改:
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
OnInitializeAsync
StateHasChanged()
收到每件物品后需要致电:
List<WeatherForecast> forecasts;
protected override async Task OnInitializedAsync()
{
forecasts =new List<WeatherForecast>();
await foreach(var forecast in ForecastService.GetForecastAsync(DateTime.Now))
{
forecasts.Add(forecast);
this.StateHasChanged();
}
}
在问题的示例中,传入的游戏可以存储在 List 中,而渲染代码保持不变:
@foreach(var game in games)
{
<p>@game</p>
}
@code {
List<string> games;
protected override async Task OnInitializedAsync()
{
games =new List<games>();
await foreach(var game in GameService.GetGamesAsync())
{
games.Add(game);
this.StateHasChanged();
}
}
}