split code and html
This commit is contained in:
parent
12e542800a
commit
c2725eba60
36
WebAssembly/PageComponents/CounterBase.cs
Normal file
36
WebAssembly/PageComponents/CounterBase.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
|
||||
namespace WebAssembly.PageComponents;
|
||||
|
||||
public class CounterBase : ComponentBase
|
||||
{
|
||||
public int CurrentCount { get; private set; }
|
||||
public bool Cancel { get; private set; }
|
||||
public Stopwatch Stopwatch { get; } = new();
|
||||
|
||||
public async Task Start()
|
||||
{
|
||||
Stopwatch.Start();
|
||||
Cancel = false;
|
||||
while (!Cancel)
|
||||
{
|
||||
CurrentCount++;
|
||||
StateHasChanged();
|
||||
await Task.Delay(10);
|
||||
}
|
||||
Stopwatch.Stop();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
Cancel = true;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Cancel = true;
|
||||
CurrentCount = 0;
|
||||
Stopwatch.Reset();
|
||||
}
|
||||
}
|
@ -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>
|
Loading…
x
Reference in New Issue
Block a user