| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- @page "/counter"
- @rendermode InteractiveServer
- @using System.Threading
- <PageTitle>Counter</PageTitle>
- <h1>Counter</h1>
- <p role="status">Current count: @currentCount</p>
- <button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
- @* @if (currentCount % 2 == 0)
- {
- <p>You win!</p>
- } *@
- @code {
- private static int currentCount = 0;
- private void IncrementCount()
- {
- // 启动后台任务进行计数
- _ = Task.Run(async () =>
- {
- while (true)
- {
- await Task.Delay(1000);
- currentCount++;
- await InvokeAsync(() =>
- {
- StateHasChanged();
- });
- }
- });
- }
- }
|