1

我在剃刀组件上的 Blazor 应用程序中制作了一个计时器。它工作正常,但我想添加其他内容,例如当计时器完成时显示一条消息,例如“完成”或“计时器完成”或任何其他消息。这是代码:

@page "/Counting"
<h1>Counter</h1>   
<p>Current count: @_currentCount</p>
<p>@value</p>
<p>Stop: @stop</p>
<p>Date: @date</p>
<p>diff: @x</p>

@code {
Timer _updateTimer;
int _currentCount = 0;
TimeSpan value= new TimeSpan(0,0,0);
DateTime date;
DateTime stop;
double x;

public void Dispose()
    {
       //_updateTimer.Dispose();
    }
  protected override async Task OnInitializedAsync()
{
  _updateTimer = new Timer(state => { InvokeAsync(UpdateCounter); }, null, 0, 500);
  await Counter();
}
 async Task UpdateCounter()
{
    //_currentCount = 0;
   //await Task.Delay(1000);
   _currentCount++;
   await Task.Delay(1000);
   //_currentCount++;
   StateHasChanged();
 }
  async Task Counter()
{
   while (true)
{
   date = DateTime.Now;
   stop = new DateTime(2020,3,21, 12,20,0);
   value = value.Add(new TimeSpan(0, 0, 1));
   x = (stop - date).TotalSeconds;
   if (x < 0  )
        {
            return;
        }
       await Task.Delay(1000);
       StateHasChanged();
     }
    }
   }  
4

1 回答 1

1

在您的剃须刀组件中:

@if (x < 0)
{
    <div class="alert alert-info">Timer is done</div>
}
于 2020-03-21T15:34:23.410 回答