mirror of
https://github.com/wwiinnddyy/LanMountainDesktop.git
synced 2026-06-21 16:14:28 +08:00
48 lines
1.1 KiB
C#
48 lines
1.1 KiB
C#
|
|
using System;
|
||
|
|
using System.Globalization;
|
||
|
|
using Avalonia;
|
||
|
|
using Avalonia.Controls;
|
||
|
|
using Avalonia.Threading;
|
||
|
|
|
||
|
|
namespace LanMontainDesktop.Views.Components;
|
||
|
|
|
||
|
|
public partial class ClockWidget : UserControl
|
||
|
|
{
|
||
|
|
private readonly DispatcherTimer _timer = new()
|
||
|
|
{
|
||
|
|
Interval = TimeSpan.FromSeconds(1)
|
||
|
|
};
|
||
|
|
|
||
|
|
public ClockWidget()
|
||
|
|
{
|
||
|
|
InitializeComponent();
|
||
|
|
|
||
|
|
_timer.Tick += OnTimerTick;
|
||
|
|
AttachedToVisualTree += OnAttachedToVisualTree;
|
||
|
|
DetachedFromVisualTree += OnDetachedFromVisualTree;
|
||
|
|
UpdateClock();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
|
||
|
|
{
|
||
|
|
UpdateClock();
|
||
|
|
_timer.Start();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
|
||
|
|
{
|
||
|
|
_timer.Stop();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnTimerTick(object? sender, EventArgs e)
|
||
|
|
{
|
||
|
|
UpdateClock();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void UpdateClock()
|
||
|
|
{
|
||
|
|
var now = DateTime.Now;
|
||
|
|
TimeTextBlock.Text = now.ToString("HH:mm:ss", CultureInfo.CurrentCulture);
|
||
|
|
}
|
||
|
|
}
|