@@ -74,12 +74,12 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
7474
7575 if ( ShowAsDateTime )
7676 {
77- StopTimer ( ) ;
77+ _refreshTimer ? . Stop ( ) ;
7878 HorizontalAlignment = HorizontalAlignment . Left ;
7979 }
8080 else
8181 {
82- StartTimer ( ) ;
82+ _refreshTimer ? . Start ( ) ;
8383 HorizontalAlignment = HorizontalAlignment . Center ;
8484 }
8585 }
@@ -94,14 +94,27 @@ protected override void OnLoaded(RoutedEventArgs e)
9494 {
9595 base . OnLoaded ( e ) ;
9696
97- if ( ! ShowAsDateTime )
98- StartTimer ( ) ;
97+ _refreshTimer = new DispatcherTimer ( ) ;
98+ _refreshTimer . Interval = TimeSpan . FromSeconds ( 10 ) ;
99+ _refreshTimer . Tag = this ;
100+ _refreshTimer . Tick += ( o , _ ) =>
101+ {
102+ if ( o is DispatcherTimer { Tag : CommitTimeTextBlock textBlock } )
103+ {
104+ var text = textBlock . GetDisplayText ( ) ;
105+ if ( ! text . Equals ( textBlock . Text , StringComparison . Ordinal ) )
106+ textBlock . Text = text ;
107+ }
108+ } ;
109+ _refreshTimer . IsEnabled = ! ShowAsDateTime ;
99110 }
100111
101112 protected override void OnUnloaded ( RoutedEventArgs e )
102113 {
103114 base . OnUnloaded ( e ) ;
104- StopTimer ( ) ;
115+
116+ _refreshTimer . Tag = null ;
117+ _refreshTimer . IsEnabled = false ;
105118 }
106119
107120 protected override void OnDataContextChanged ( EventArgs e )
@@ -110,29 +123,6 @@ protected override void OnDataContextChanged(EventArgs e)
110123 SetCurrentValue ( TextProperty , GetDisplayText ( ) ) ;
111124 }
112125
113- private void StartTimer ( )
114- {
115- if ( _refreshTimer != null )
116- return ;
117-
118- _refreshTimer = DispatcherTimer . Run ( ( ) =>
119- {
120- var text = GetDisplayText ( ) ;
121- if ( ! text . Equals ( Text , StringComparison . Ordinal ) )
122- Text = text ;
123- return true ;
124- } , TimeSpan . FromSeconds ( 10 ) ) ;
125- }
126-
127- private void StopTimer ( )
128- {
129- if ( _refreshTimer != null )
130- {
131- _refreshTimer . Dispose ( ) ;
132- _refreshTimer = null ;
133- }
134- }
135-
136126 private string GetDisplayText ( )
137127 {
138128 var timestamp = Timestamp ;
@@ -185,6 +175,6 @@ private string GetDisplayText()
185175 private bool _use24Hours = true ;
186176 private int _dateTimeFormat = 0 ;
187177 private ulong _timestamp = 0 ;
188- private IDisposable _refreshTimer = null ;
178+ private DispatcherTimer _refreshTimer = null ;
189179 }
190180}
0 commit comments