Skip to content

Workaround ColoPicker AoT issue #714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion components/ColorPicker/src/ColorPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace CommunityToolkit.WinUI.Controls;
[TemplatePart(Name = nameof(ColorPicker.CheckeredBackground9Border), Type = typeof(Border))]
[TemplatePart(Name = nameof(ColorPicker.CheckeredBackground10Border), Type = typeof(Border))]
[TemplatePart(Name = nameof(ColorPicker.ColorPanelSelector), Type = typeof(Segmented))]
[TemplatePart(Name = nameof(ColorPicker.ContentContainer), Type = typeof(SwitchPresenter))]
[TemplatePart(Name = nameof(ColorPicker.ColorSpectrumControl), Type = typeof(ColorSpectrum))]
[TemplatePart(Name = nameof(ColorPicker.ColorSpectrumAlphaSlider), Type = typeof(ColorPickerSlider))]
[TemplatePart(Name = nameof(ColorPicker.ColorSpectrumThirdDimensionSlider), Type = typeof(ColorPickerSlider))]
Expand Down Expand Up @@ -74,7 +75,9 @@ public partial class ColorPicker : Microsoft.UI.Xaml.Controls.ColorPicker
private Color? updatedRgbColor = null;
private DispatcherQueueTimer? dispatcherQueueTimer = null;

private Segmented ColorPanelSelector;
private Segmented ColorPanelSelector;
private SwitchPresenter ContentContainer;

private ColorSpectrum ColorSpectrumControl;
private ColorPickerSlider ColorSpectrumAlphaSlider;
private ColorPickerSlider ColorSpectrumThirdDimensionSlider;
Expand Down Expand Up @@ -177,6 +180,7 @@ protected override void OnApplyTemplate()
this.ConnectEvents(false);

this.ColorPanelSelector = (Segmented)GetTemplateChild(nameof(ColorPanelSelector));
this.ContentContainer = (SwitchPresenter)GetTemplateChild(nameof(ContentContainer));

this.ColorSpectrumControl = (ColorSpectrum)GetTemplateChild(nameof(ColorSpectrumControl));
this.ColorSpectrumAlphaSlider = (ColorPickerSlider)this.GetTemplateChild(nameof(ColorSpectrumAlphaSlider));
Expand Down Expand Up @@ -255,6 +259,8 @@ private void ConnectEvents(bool connected)
this.eventsConnected == false)
{
// Add all events
if (this.ColorPanelSelector != null) { this.ColorPanelSelector.SelectionChanged += this.ColorPanelSelector_SelectionChanged; }

if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.ColorChanged += ColorSpectrum_ColorChanged; }
if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.GotFocus += ColorSpectrum_GotFocus; }
if (this.HexInputTextBox != null) { this.HexInputTextBox.KeyDown += HexInputTextBox_KeyDown; }
Expand Down Expand Up @@ -299,6 +305,8 @@ private void ConnectEvents(bool connected)
this.eventsConnected == true)
{
// Remove all events
if (this.ColorPanelSelector != null) { this.ColorPanelSelector.SelectionChanged -= this.ColorPanelSelector_SelectionChanged; }

if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.ColorChanged -= ColorSpectrum_ColorChanged; }
if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.GotFocus -= ColorSpectrum_GotFocus; }
if (this.HexInputTextBox != null) { this.HexInputTextBox.KeyDown -= HexInputTextBox_KeyDown; }
Expand Down Expand Up @@ -1186,6 +1194,22 @@ private void CustomPaletteColors_CollectionChanged(object? sender, NotifyCollect
return;
}

/// <summary>
/// Event handler for when the color panel selector selection changes.
/// We are setting the value here instead of ElementName binding as a workaround for AoT issues.
/// (See https://github.com/microsoft/microsoft-ui-xaml/issues/10214)
/// </summary>
private void ColorPanelSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.ContentContainer is null ||
(sender as Segmented)?.SelectedItem is not FrameworkElement selectedItem)
{
return;
}

this.ContentContainer.Value = selectedItem.Name;
}

/// <summary>
/// Event handler for when the color spectrum color is changed.
/// This occurs when the user presses on the spectrum to select a new color.
Expand Down
3 changes: 1 addition & 2 deletions components/ColorPicker/src/ColorPicker.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@
</controls:Segmented>

<controls:SwitchPresenter x:Name="ContentContainer"
Grid.Row="2"
Value="{Binding ElementName=ColorPanelSelector, Path=SelectedItem.Name}">
Grid.Row="2">
<controls:Case Value="SpectrumItem">
<Grid HorizontalAlignment="Stretch"
animations:Implicit.HideAnimations="{StaticResource HideTransitions}"
Expand Down
Loading