TimerWatch.xaml.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. namespace HistoryView.Controls.Basics;
  2. public partial class TimerWatch : UserControl
  3. {
  4. public TimerWatch()
  5. {
  6. InitializeComponent();
  7. _timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTmrTrg);
  8. _timer.Interval = 1000;
  9. _timer.AutoReset = true;
  10. _timer.Enabled = true;
  11. }
  12. private readonly System.Timers.Timer _timer = new();
  13. private void OnTmrTrg(object? sender, System.Timers.ElapsedEventArgs e)
  14. {
  15. try
  16. {
  17. App.Current?.Dispatcher?.Invoke(() =>
  18. {
  19. this.Date.Text = DateTime.Now.ToString("yyyy/MM/dd");
  20. this.Time.Text = DateTime.Now.ToString("HH:mm:ss");
  21. });
  22. }
  23. catch
  24. {
  25. }
  26. }
  27. public Orientation Orientation
  28. {
  29. get { return (Orientation)GetValue(OrientationProperty); }
  30. set { SetValue(OrientationProperty, value); }
  31. }
  32. public static readonly DependencyProperty OrientationProperty =
  33. DependencyProperty.Register("Orientation", typeof(Orientation), typeof(TimerWatch), new PropertyMetadata(default));
  34. public SolidColorBrush TextBrush
  35. {
  36. get { return (SolidColorBrush)GetValue(TextBrushProperty); }
  37. set { SetValue(TextBrushProperty, value); }
  38. }
  39. // Using a DependencyProperty as the backing store for TextBrush. This enables animation, styling, binding, etc...
  40. public static readonly DependencyProperty TextBrushProperty =
  41. DependencyProperty.Register("TextBrush", typeof(SolidColorBrush), typeof(TimerWatch), new PropertyMetadata(new SolidColorBrush()));
  42. }