feat: add auto tick sample.

This commit is contained in:
rabbitism
2024-04-22 02:05:29 +08:00
parent 3815a5114c
commit db6ba785dd
4 changed files with 161 additions and 23 deletions

View File

@@ -1,6 +1,31 @@
namespace Ursa.Demo.ViewModels;
using System;
using System.Timers;
using CommunityToolkit.Mvvm.ComponentModel;
public class ClockDemoViewModel
namespace Ursa.Demo.ViewModels;
public partial class ClockDemoViewModel: ObservableObject, IDisposable
{
private Timer _timer;
[ObservableProperty] private DateTime _time;
public ClockDemoViewModel()
{
Time = DateTime.Now;
_timer = new Timer(1000);
_timer.Elapsed += TimerOnElapsed;
_timer.Start();
}
private void TimerOnElapsed(object? sender, ElapsedEventArgs e)
{
Time = DateTime.Now;
}
public void Dispose()
{
_timer.Stop();
_timer.Elapsed -= TimerOnElapsed;
_timer.Dispose();
}
}