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

@@ -5,10 +5,14 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="https://irihi.tech/ursa"
xmlns:vm="clr-namespace:Ursa.Demo.ViewModels"
x:DataType="vm:ClockDemoViewModel"
x:CompileBindings="True"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Grid>
<u:Clock HorizontalAlignment="Left"></u:Clock>
<u:Clock HorizontalAlignment="Left" Time="{Binding Time}"></u:Clock>
</Grid>
</UserControl>

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();
}
}