Windows icon indicating copy to clipboard operation
Windows copied to clipboard

ColorPicker sample in gallery crashing under Windows App SDK

Open Arlodotexe opened this issue 1 year ago • 6 comments

Describe the bug

When running the gallery with the Wasdk head on the latest main commit, the ColorPicker sample will crash the app:

image

   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|38_0(Int32 hr)
   at ABI.Microsoft.UI.Xaml.IFrameworkElementOverridesMethods.MeasureOverride(IObjectReference _obj, Size availableSize)
   at Microsoft.UI.Xaml.FrameworkElement.MeasureOverride(Size availableSize)
   at Microsoft.UI.Xaml.FrameworkElement.Microsoft.UI.Xaml.IFrameworkElementOverrides.MeasureOverride(Size availableSize)
   at ABI.Microsoft.UI.Xaml.IFrameworkElementOverrides.Do_Abi_MeasureOverride_0(IntPtr thisPtr, Size availableSize, Size* result)

This crash does not occur under UWP.

Steps to reproduce

1. Clone the repo
2. Generate the gallery solution, include the ColorPicker component and the wasdk multitarget/head.
3. Build and deploy the wasdk gallery 
4. Navigate to the ColorPicker sample page and observe crash.

Expected behavior

No crash.

Screenshots

No response

Code Platform

  • [ ] UWP
  • [X] WinAppSDK / WinUI 3
  • [ ] Web Assembly (WASM)
  • [ ] Android
  • [ ] iOS
  • [ ] MacOS
  • [ ] Linux / GTK

Windows Build Number

  • [ ] Windows 10 1809 (Build 17763)
  • [ ] Windows 10 1903 (Build 18362)
  • [ ] Windows 10 1909 (Build 18363)
  • [ ] Windows 10 2004 (Build 19041)
  • [ ] Windows 10 20H2 (Build 19042)
  • [ ] Windows 10 21H1 (Build 19043)
  • [ ] Windows 10 21H2 (Build 19044)
  • [ ] Windows 10 22H2 (Build 19045)
  • [ ] Windows 11 21H2 (Build 22000)
  • [ ] Other (specify)

Other Windows Build number

No response

App minimum and target SDK version

  • [ ] Windows 10, version 1809 (Build 17763)
  • [ ] Windows 10, version 1903 (Build 18362)
  • [ ] Windows 10, version 1909 (Build 18363)
  • [ ] Windows 10, version 2004 (Build 19041)
  • [ ] Windows 10, version 2104 (Build 20348)
  • [ ] Windows 11, version 22H2 (Build 22000)
  • [ ] Other (specify)

Other SDK version

No response

Visual Studio Version

No response

Visual Studio Build Number

No response

Device form factor

No response

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item.

Arlodotexe avatar Sep 18 '24 18:09 Arlodotexe

Has the same error message as https://github.com/CommunityToolkit/Windows/issues/517

Arlodotexe avatar Sep 18 '24 20:09 Arlodotexe

Additional info uncovered thanks to a tip from @Sergio0694

image

(Possibly related to https://github.com/CommunityToolkit/Windows/issues/516? Though, not the same error message.)

@michael-hawker Anything you could add? SwitchPresenter may be having more than one AoT issue here.

Arlodotexe avatar Sep 18 '24 20:09 Arlodotexe

@Arlodotexe Switch Presenter uses all the general types and such from XAML like DependencyObjectCollection and DependencyObject - It also just does a lot of general comparison/reflection-y type stuff when trying to compare values here: https://github.com/CommunityToolkit/Windows/blob/main/components/Primitives/src/SwitchPresenter/SwitchPresenter.cs#L116

So, there's probably something there to figure out how to better guard against?

I also saw a new issue on the WinUI repo about controls and resource dictionaries + trimming, so something we may want to keep an eye on incase there's other underlying issues still we need to be concerned about or bubble up with the platform team. RE: https://github.com/microsoft/microsoft-ui-xaml/issues/10020

michael-hawker avatar Sep 18 '24 20:09 michael-hawker

Since #516 turned out to be an issue with the EnumValuesExtension, I suspect this may be a binding issue given the data in the exception. Could also be erroring internally in the setter, but the Value property is a good place to start looking.

Arlodotexe avatar Oct 17 '24 15:10 Arlodotexe

It looks like the problem is here: https://github.com/CommunityToolkit/Windows/blob/7ab8a422eb20b8829091808e192a389087195f00/components/ColorPicker/src/ColorPicker.xaml?plain=1#L238-L240

Unfortunately, this isn't a backed resource dictionary, so we can't use x:Bind here. If we try, we get the error:

1>V:\Windows\components\ColorPicker\src\ColorPicker.xaml(240,55): XamlCompiler error WMC1119: This Xaml file must have a code-behind class to use {x:Bind}. See http://go.microsoft.com/fwlink/?LinkID=532920&clcid=0x409 for more information
1>Done building project "CommunityToolkit.WinUI.Controls.ColorPicker.csproj" -- FAILED.

To fix the problem, we'll need to locate any .xaml files that use a non-TemplatedParent variant of {Binding}, update the resource dictionary to be backed by a cs file, and update bindings to use x:Bind.

Arlodotexe avatar Nov 04 '24 18:11 Arlodotexe

This issue got opened on the WinUI repo which is related https://github.com/microsoft/microsoft-ui-xaml/issues/10214 - not too sure what we can do here atm.

michael-hawker avatar Dec 06 '24 07:12 michael-hawker

The SwitchPresenter only needs the Name of the selected item from the Segmented control. Since this Segemented control is already accessible as a TemplatedPart, why not just also include the SwitchPresenter as a TemplatedPart and implement the selection logic in code? That should at least prevent crashes.

AndrewKeepCoding avatar Jul 17 '25 09:07 AndrewKeepCoding

Yes, it worked with a few lines of code:

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;
}

@Arlodotexe @michael-hawker Thoughts?

AndrewKeepCoding avatar Jul 17 '25 09:07 AndrewKeepCoding

Thanks @AndrewKeepCoding, I suppose that could work for now; we could add a comment pointing to the WinUI issue: https://github.com/microsoft/microsoft-ui-xaml/issues/10214

michael-hawker avatar Jul 18 '25 18:07 michael-hawker

Hi @michael-hawker ! Should I be assigned to create a PR for this? or can I go ahead and create one?

AndrewKeepCoding avatar Jul 19 '25 01:07 AndrewKeepCoding