Skip to content

Commit fae8940

Browse files
AndrewKeepCodingArlodotexe
authored andcommitted
Work around AoT issue by setting ColorPicker value in code instead of using XAML binding
1 parent a784bad commit fae8940

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

components/ColorPicker/src/ColorPicker.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace CommunityToolkit.WinUI.Controls;
4242
[TemplatePart(Name = nameof(ColorPicker.CheckeredBackground9Border), Type = typeof(Border))]
4343
[TemplatePart(Name = nameof(ColorPicker.CheckeredBackground10Border), Type = typeof(Border))]
4444
[TemplatePart(Name = nameof(ColorPicker.ColorPanelSelector), Type = typeof(Segmented))]
45+
[TemplatePart(Name = nameof(ColorPicker.ContentContainer), Type = typeof(SwitchPresenter))]
4546
[TemplatePart(Name = nameof(ColorPicker.ColorSpectrumControl), Type = typeof(ColorSpectrum))]
4647
[TemplatePart(Name = nameof(ColorPicker.ColorSpectrumAlphaSlider), Type = typeof(ColorPickerSlider))]
4748
[TemplatePart(Name = nameof(ColorPicker.ColorSpectrumThirdDimensionSlider), Type = typeof(ColorPickerSlider))]
@@ -74,7 +75,9 @@ public partial class ColorPicker : Microsoft.UI.Xaml.Controls.ColorPicker
7475
private Color? updatedRgbColor = null;
7576
private DispatcherQueueTimer? dispatcherQueueTimer = null;
7677

77-
private Segmented ColorPanelSelector;
78+
private Segmented ColorPanelSelector;
79+
private SwitchPresenter ContentContainer;
80+
7881
private ColorSpectrum ColorSpectrumControl;
7982
private ColorPickerSlider ColorSpectrumAlphaSlider;
8083
private ColorPickerSlider ColorSpectrumThirdDimensionSlider;
@@ -177,6 +180,7 @@ protected override void OnApplyTemplate()
177180
this.ConnectEvents(false);
178181

179182
this.ColorPanelSelector = (Segmented)GetTemplateChild(nameof(ColorPanelSelector));
183+
this.ContentContainer = (SwitchPresenter)GetTemplateChild(nameof(ContentContainer));
180184

181185
this.ColorSpectrumControl = (ColorSpectrum)GetTemplateChild(nameof(ColorSpectrumControl));
182186
this.ColorSpectrumAlphaSlider = (ColorPickerSlider)this.GetTemplateChild(nameof(ColorSpectrumAlphaSlider));
@@ -255,6 +259,8 @@ private void ConnectEvents(bool connected)
255259
this.eventsConnected == false)
256260
{
257261
// Add all events
262+
if (this.ColorPanelSelector != null) { this.ColorPanelSelector.SelectionChanged += this.ColorPanelSelector_SelectionChanged; }
263+
258264
if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.ColorChanged += ColorSpectrum_ColorChanged; }
259265
if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.GotFocus += ColorSpectrum_GotFocus; }
260266
if (this.HexInputTextBox != null) { this.HexInputTextBox.KeyDown += HexInputTextBox_KeyDown; }
@@ -299,6 +305,8 @@ private void ConnectEvents(bool connected)
299305
this.eventsConnected == true)
300306
{
301307
// Remove all events
308+
if (this.ColorPanelSelector != null) { this.ColorPanelSelector.SelectionChanged -= this.ColorPanelSelector_SelectionChanged; }
309+
302310
if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.ColorChanged -= ColorSpectrum_ColorChanged; }
303311
if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.GotFocus -= ColorSpectrum_GotFocus; }
304312
if (this.HexInputTextBox != null) { this.HexInputTextBox.KeyDown -= HexInputTextBox_KeyDown; }
@@ -1186,6 +1194,22 @@ private void CustomPaletteColors_CollectionChanged(object? sender, NotifyCollect
11861194
return;
11871195
}
11881196

1197+
/// <summary>
1198+
/// Event handler for when the color panel selector selection changes.
1199+
/// We are setting the value here instead of ElementName binding as a workaround for AoT issues.
1200+
/// (See https://github.com/microsoft/microsoft-ui-xaml/issues/10214)
1201+
/// </summary>
1202+
private void ColorPanelSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
1203+
{
1204+
if (this.ContentContainer is null ||
1205+
(sender as Segmented)?.SelectedItem is not FrameworkElement selectedItem)
1206+
{
1207+
return;
1208+
}
1209+
1210+
this.ContentContainer.Value = selectedItem.Name;
1211+
}
1212+
11891213
/// <summary>
11901214
/// Event handler for when the color spectrum color is changed.
11911215
/// This occurs when the user presses on the spectrum to select a new color.

components/ColorPicker/src/ColorPicker.xaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@
237237
</controls:Segmented>
238238

239239
<controls:SwitchPresenter x:Name="ContentContainer"
240-
Grid.Row="2"
241-
Value="{Binding ElementName=ColorPanelSelector, Path=SelectedItem.Name}">
240+
Grid.Row="2">
242241
<controls:Case Value="SpectrumItem">
243242
<Grid HorizontalAlignment="Stretch"
244243
animations:Implicit.HideAnimations="{StaticResource HideTransitions}"

0 commit comments

Comments
 (0)