@@ -34,19 +34,19 @@ public partial class FocusTracker : Control
34
34
35
35
private static void OnIsActiveChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
36
36
{
37
- var focusTracker = d as FocusTracker ;
38
-
39
- if ( e . NewValue != null && ( bool ) e . NewValue )
37
+ if ( d is FocusTracker focusTracker )
40
38
{
41
- focusTracker ? . Start ( ) ;
42
- }
43
- else
44
- {
45
- focusTracker ? . Stop ( ) ;
39
+ if ( e . NewValue != null && ( bool ) e . NewValue )
40
+ {
41
+ focusTracker . Start ( ) ;
42
+ }
43
+ else
44
+ {
45
+ focusTracker . Stop ( ) ;
46
+ }
46
47
}
47
48
}
48
49
49
- private DispatcherQueueTimer updateTimer ;
50
50
private TextBlock controlName ;
51
51
private TextBlock controlType ;
52
52
private TextBlock controlAutomationName ;
@@ -69,32 +69,42 @@ public FocusTracker()
69
69
DefaultStyleKey = typeof ( FocusTracker ) ;
70
70
}
71
71
72
+ /// <summary>
73
+ /// Update the visual state of the control when its template is changed.
74
+ /// </summary>
75
+ protected override void OnApplyTemplate ( )
76
+ {
77
+ controlName = GetTemplateChild ( "ControlName" ) as TextBlock ;
78
+ controlType = GetTemplateChild ( "ControlType" ) as TextBlock ;
79
+ controlAutomationName = GetTemplateChild ( "ControlAutomationName" ) as TextBlock ;
80
+ controlFirstParentWithName = GetTemplateChild ( "ControlFirstParentWithName" ) as TextBlock ;
81
+ }
82
+
72
83
private void Start ( )
73
84
{
74
- if ( updateTimer == null )
85
+ // Get currently focused control once when we start
86
+ if ( Windows . Foundation . Metadata . ApiInformation . IsPropertyPresent ( "Windows.UI.Xaml.UIElement" , "XamlRoot" ) && XamlRoot != null )
75
87
{
76
- updateTimer = DispatcherQueue . GetForCurrentThread ( ) . CreateTimer ( ) ;
77
- updateTimer . Tick += UpdateTimer_Tick ;
88
+ FocusOnControl ( FocusManager . GetFocusedElement ( XamlRoot ) as FrameworkElement ) ;
89
+ }
90
+ else
91
+ {
92
+ FocusOnControl ( FocusManager . GetFocusedElement ( ) as FrameworkElement ) ;
78
93
}
79
94
80
- updateTimer . Start ( ) ;
95
+ // Then use FocusManager event from 1809 to listen to updates
96
+ FocusManager . GotFocus += FocusManager_GotFocus ;
81
97
}
82
98
83
99
private void Stop ( )
84
100
{
85
- updateTimer ? . Stop ( ) ;
101
+ FocusManager . GotFocus -= FocusManager_GotFocus ;
86
102
ClearContent ( ) ;
87
103
}
88
104
89
- /// <summary>
90
- /// Update the visual state of the control when its template is changed.
91
- /// </summary>
92
- protected override void OnApplyTemplate ( )
105
+ private void FocusManager_GotFocus ( object sender , FocusManagerGotFocusEventArgs e )
93
106
{
94
- controlName = GetTemplateChild ( "ControlName" ) as TextBlock ;
95
- controlType = GetTemplateChild ( "ControlType" ) as TextBlock ;
96
- controlAutomationName = GetTemplateChild ( "ControlAutomationName" ) as TextBlock ;
97
- controlFirstParentWithName = GetTemplateChild ( "ControlFirstParentWithName" ) as TextBlock ;
107
+ FocusOnControl ( e . NewFocusedElement as FrameworkElement ) ;
98
108
}
99
109
100
110
private void ClearContent ( )
@@ -105,19 +115,8 @@ private void ClearContent()
105
115
controlFirstParentWithName . Text = string . Empty ;
106
116
}
107
117
108
- private void UpdateTimer_Tick ( object sender , object e )
118
+ private void FocusOnControl ( FrameworkElement focusedControl )
109
119
{
110
- FrameworkElement focusedControl ;
111
-
112
- if ( Windows . Foundation . Metadata . ApiInformation . IsPropertyPresent ( "Windows.UI.Xaml.UIElement" , "XamlRoot" ) && XamlRoot != null )
113
- {
114
- focusedControl = FocusManager . GetFocusedElement ( XamlRoot ) as FrameworkElement ;
115
- }
116
- else
117
- {
118
- focusedControl = FocusManager . GetFocusedElement ( ) as FrameworkElement ;
119
- }
120
-
121
120
if ( focusedControl == null )
122
121
{
123
122
ClearContent ( ) ;
0 commit comments