split code and html

This commit is contained in:
2022-01-22 22:26:20 +01:00
parent 12e542800a
commit c2725eba60
2 changed files with 40 additions and 35 deletions

View File

@ -1,45 +1,14 @@
@page "/counter"
@using System.Diagnostics
@inherits WebAssembly.PageComponents.CounterBase
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status">Current count: @_currentCount</p>
<p role="status">Runtime: @_stopwatch.Elapsed.ToString("g")</p>
<p role="status">Current count: @CurrentCount</p>
<p role="status">Runtime: @Stopwatch.Elapsed.ToString("G")</p>
<button class="btn btn-success" @onclick="Start">Start</button>
<button class="btn btn-danger" @onclick="Stop">Stop</button>
<button class="btn btn-secondary" @onclick="Reset">Reset</button>
@code {
private int _currentCount = 0;
private bool _cancel;
private readonly Stopwatch _stopwatch = new();
private async Task Start()
{
_stopwatch.Start();
_cancel = false;
while (!_cancel)
{
_currentCount++;
StateHasChanged();
await Task.Delay(10);
}
_stopwatch.Stop();
}
private void Stop()
{
_cancel = true;
}
private void Reset()
{
_cancel = true;
_currentCount = 0;
_stopwatch.Reset();
}
}
<button class="btn btn-secondary" @onclick="Reset">Reset</button>