-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataAsyncProperty.CancellationToken.razor
More file actions
44 lines (38 loc) · 1.84 KB
/
DataAsyncProperty.CancellationToken.razor
File metadata and controls
44 lines (38 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@page "/DataAsyncUsingCancellationToken"
@inject WeatherForecastService WeatherForecastService
<h2>DataAsync: Cancellation Token</h2>
<p>
This example uses the <a class="helplink" href="https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxDataGrid-1.DataAsync" target="_blank">DataAsync</a> property to bind the Data Grid to a strongly typed collection that is loaded asynchronously. The <b>DataAsync</b> delegate accepts a cancellation token as a parameter. In this example, the token receives a cancellation signal when the <b>Data Grid</b> tab is closed. When you switch between tabs before grid data is loaded, the cancelled task count is increased.
</p>
<p>
Note that the <b>Render GUID</b> does not change during page initialization. This means the page is rendered only once.
</p>
<p>
<b>Render GUID</b>: @Guid.NewGuid()
</p>
<DxTabs>
<DxTabPage Text="Data Grid">
<DxDataGrid DataAsync="@LoadDataAsync" KeyFieldName="Id">
<Columns>
<DxDataGridColumn Field="@nameof(WeatherForecast.Summary)" />
<DxDataGridDateEditColumn Field="@nameof(WeatherForecast.Date)" />
<DxDataGridSpinEditColumn Field="@nameof(WeatherForecast.TemperatureC)" />
<DxDataGridSpinEditColumn Field="@nameof(WeatherForecast.TemperatureF)" />
</Columns>
</DxDataGrid>
</DxTabPage>
<DxTabPage Text="Cancelled Task Counter">
Cancelled Task Count: @CancelledTaskCount
</DxTabPage>
</DxTabs>
@code {
int CancelledTaskCount = 0;
async Task<IEnumerable<WeatherForecast>> LoadDataAsync(CancellationToken token) {
try {
return await WeatherForecastService.GetForecastAsync(DateTime.Now.Date, token);
} catch (TaskCanceledException) {
CancelledTaskCount++;
}
return Enumerable.Empty<WeatherForecast>();
}
}