CommunityToolkit / Windows

Collection of controls for WinUI 2, WinUI 3, and Uno Platform developers. Simplifies and demonstrates common developer tasks building experiences for Windows with .NET.
https://aka.ms/windowstoolkitdocs
Other
596 stars 74 forks source link

StaggeredPanel, WrapPanel samples in gallery crash under Windows App SDK #517

Open Arlodotexe opened 2 months ago

Arlodotexe commented 2 months ago

Describe the bug

When running the gallery with the Wasdk head on the latest main commit, the 'StaggeredPanel' and 'WrapPanel' samples will crash the app with the same error message and stack trace: image

Exception message:

System.Runtime.InteropServices.COMException

Stack trace:

   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 behavior is not present when running the gallery under UWP.

Steps to reproduce

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

Expected behavior

No crash

Screenshots

No response

Code Platform

Windows Build Number

Other Windows Build number

No response

App minimum and target SDK version

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 commented 2 months ago

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

michael-hawker commented 1 month ago

@Arlodotexe is this specific to AOT or just updating from 1.5 to 1.6? If the later, then it seems like it'd be a regression in the SDK, as I don't think we've modified this code between releases, eh?

Arlodotexe commented 1 month ago

WrapPanel and StaggeredPanel seem to work fine under WindowsAppSDK 1.6 with AoT disabled:

Image

Image

Arlodotexe commented 1 month ago

Additional information pulled from the unhandled exception:

Image

Arlodotexe commented 1 month ago

This code is currently using:

https://github.com/CommunityToolkit/Windows/blob/665ba24405f543242e5ff2b0d7d24c045d6836fb/components/Primitives/samples/StaggeredPanelSample.xaml#L42-L44

Removing the binding on the ItemsPanelTemplate fixes the issue:

Image

Unfortunately, we're not able to use x:Bind here, as any usage results in:

1>StaggeredPanelSample.xaml(42,22): XamlCompiler error WMC1111: DataTemplates containing x:Bind need a DataType to be specified using 'x:DataType'
1>Done building project "Primitives.Samples.csproj" -- FAILED.

Neither a DataTemplate nor its accompanying x:DataType can be added here since ItemsPanelTemplate is already a template and must have a first child of type Panel.

Looking through documentation for 'official' examples of binding properties on an ItemsPanelTemplate, I'm not finding any where these properties are bound, not using {Binding}, {x:Bind} or {TemplateBinding}.

{Binding} worked for us before, but it's not AoT safe. What is the recommended approach here?

michael-hawker commented 1 month ago

@Arlodotexe yeah, this is a gap with ItemsPanelTemplate. In most cases you set this without binding, we're doing something different with the sample system here (though in some of my projects I've occasionally also used Binding here).

There's not an issue for this filed in the WinUI repo (that I could find), we should probably bubble this up there for tracking at least for AoT and x:Bind improvements.

Though it is also highly related/dependent/tied to https://github.com/microsoft/microsoft-ui-xaml/issues/2508 as well; as in this case you'd never have a DataType/item like a traditional DataTemplate, the context here is still the main context of the parent control/page even though it's a "DataTemplate" for the panel... so it should be breaking out to the outer context for looking for what would make sense to bind to (just as if you were trying to bind a command of your ViewModel to an inner item template, which is broken still today šŸ˜¢).

Arlodotexe commented 1 month ago

Filed an issue https://github.com/microsoft/microsoft-ui-xaml/issues/10070