@@ -15,22 +15,27 @@ namespace Files.App.Controls
1515 /// A control for a State and Color aware Icon
1616 /// </summary>
1717 public partial class ThemedIcon : Control
18- {
18+ {
19+ private Viewbox? _filledViewBox;
20+ private Viewbox? _outlineViewBox;
21+ private Viewbox? _layeredViewBox;
22+ private Canvas? _layeredCanvas;
23+
1924 public ThemedIcon()
2025 {
2126 DefaultStyleKey = typeof(ThemedIcon);
2227 }
2328
2429 protected override void OnApplyTemplate()
2530 {
26- IsEnabledChanged -= OnIsEnabledChanged;
27-
2831 base.OnApplyTemplate();
2932
3033 IsEnabledChanged += OnIsEnabledChanged;
3134
3235 _isOwnerEnabled = IsEnabled;
3336
37+ GetTemplateParts();
38+
3439 FindOwnerControlStates();
3540 OnFilledIconChanged();
3641 OnOutlineIconChanged();
@@ -40,39 +45,49 @@ protected override void OnApplyTemplate()
4045 OnIconColorTypeChanged();
4146 }
4247
48+ private void GetTemplateParts()
49+ {
50+ // Gets the template parts and sets the private fields
51+ _outlineViewBox = GetTemplateChild( OutlinePathIconViewBox ) as Viewbox;
52+ _filledViewBox = GetTemplateChild( FilledPathIconViewBox ) as Viewbox;
53+ _layeredViewBox = GetTemplateChild( LayeredPathIconViewBox ) as Viewbox;
54+
55+ _layeredCanvas = GetTemplateChild( LayeredPathCanvas ) as Canvas;
56+ }
57+
4358 // Updates paths and layers
4459
4560 private void OnFilledIconChanged()
4661 {
4762 // Updates Filled Icon from Path Data
48- if (GetTemplateChild(FilledPathIconViewBox) is not Viewbox filledViewBox )
63+ if (_filledViewBox == null )
4964 return;
5065
51- SetPathData(FilledIconPath, FilledIconData ?? string.Empty, filledViewBox );
66+ SetPathData(FilledIconPath, FilledIconData ?? string.Empty, _filledViewBox );
5267 }
5368
5469 private void OnOutlineIconChanged()
5570 {
5671 // Updates Outline Icon from Path Data
57- if (GetTemplateChild(OutlinePathIconViewBox) is not Viewbox outlineViewBox )
72+ if (_outlineViewBox == null )
5873 return;
5974
60- SetPathData(OutlineIconPath, OutlineIconData ?? string.Empty, outlineViewBox );
75+ SetPathData(OutlineIconPath, OutlineIconData ?? string.Empty, _outlineViewBox );
6176 }
6277
6378 private void OnLayeredIconChanged()
6479 {
6580 // Updates Layered Icon from it's Layers
66- if (GetTemplateChild(LayeredPathIconViewBox) is not Viewbox layeredViewBox ||
67- GetTemplateChild(LayeredPathCanvas) is not Canvas canvas ||
68- Layers is not ICollection<ThemedIconLayer> layers)
69- return;
81+ if ( _layeredViewBox == null ||
82+ _layeredCanvas == null ||
83+ Layers is not ICollection<ThemedIconLayer> layers)
84+ return;
7085
71- canvas .Children.Clear();
86+ _layeredCanvas .Children.Clear();
7287
7388 foreach (var layer in layers)
7489 {
75- canvas .Children.Add(
90+ _layeredCanvas .Children.Add(
7691 new ThemedIconLayer()
7792 {
7893 LayerType = layer.LayerType,
@@ -180,8 +195,8 @@ private void OnIconColorTypeChanged()
180195 }
181196
182197 // Update layered icon color
183- if (GetTemplateChild(LayeredPathCanvas) is Canvas canvas )
184- foreach (var layer in canvas .Children.Cast<ThemedIconLayer>())
198+ if (_layeredCanvas != null )
199+ foreach (var layer in _layeredCanvas .Children.Cast<ThemedIconLayer>())
185200 layer.IconColorType = IconColorType;
186201 }
187202 else
0 commit comments