-
Notifications
You must be signed in to change notification settings - Fork 80
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
SwitchPresenter sample in gallery crashes under Windows App SDK #516
Comments
This seems not to be a These are not fixes: EnumValuesExtension.cs protected override object ProvideValue()
{
// This crashes the app.
// return Enum.GetValues(Type!);
// This does not crash the app.
return Enum.GetNames(Type!);
} or just: SwitchPresenterValueSample.xaml.cs public SwitchPresenterValueSample()
{
this.InitializeComponent();
// This crashes the app.
// this.AnimalPicker.ItemsSource = Enum.GetValues(typeof(Animal));
// This does not crash the app.
Animal[] animals = [Animal.Cat, Animal.Dog, Animal.Bunny, Animal.Llama, Animal.Parrot, Animal.Squirrel];
this.AnimalPicker.ItemsSource = animals;
} |
Looks like it's an issue with generated code for types passed into EnumValuesExtension. This call to When it reaches the This might be related to this other workaround we had to do to get XamlTypeInfo to generate on .NET Native/UWP: Though it seems like this doesn't work for NativeAot/Wasdk. @Sergio0694 What are your thoughts? |
Interesting, this works fine in my ComputeSharp sample app (UWP, .NET 9). It seems the CsWinRT generator is missing that |
For now, I'll update the sample to avoid using the Enum call. @Arlodotexe I'll close this when I open that PR for the SwitchPresenter work I'm looking at. Will use @AndrewKeepCoding's just static array approach, I think. Mind summarizing the main issues related to the intersection of things in a new issue we can use to track elsewhere? Or should we be opening something on CsWinRT? Since we have the attribute here on the Enum call, shouldn't that get flagged somewhere? Or is that a XAML Compiler thing? |
Huh, actually with the array approach @AndrewKeepCoding shared for the sample I'm still seeing a different error "Value does not fall within the expected range" (though it works with UWP still no problem...)
Maybe I won't be fixing this sample then yet... 🤔 |
Though this sample is still broken with NAOT on WinUI 3... :( Tried a couple of the suggested workarounds, but they did not work on my machine, see #516
@michael-hawker I want to reconfirm it with the latest commits but now I'm facing a bunch of compile errors and it won't even build. 😅 |
@AndrewKeepCoding thanks for the info. I based my changes on the latest |
Yeah this is strange, I'm able to repro the issue @michael-hawker was running into here. This is no longer an issue with the XAML compiler generating AoT supporting code for WinRT, something else is happening here. After a few iterations, I landed on a setup that's strongly typed and ensures no null or empty lists are being passed for binding: Sample <Page x:Class="PrimitivesExperiment.Samples.SwitchPresenter.SwitchPresenterValueSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:PrimitivesExperiment.Samples.SwitchPresenter"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="using:CommunityToolkit.WinUI"
mc:Ignorable="d">
<StackPanel>
<ComboBox x:Name="AnimalPicker"
Header="Pick an Animal"
ItemsSource="{x:Bind Items, Mode=OneWay}" />
<controls:SwitchPresenter Padding="16"
TargetType="local:Animal"
Value="{x:Bind AnimalPicker.SelectedItem, Mode=OneWay}">
<controls:Case Value="Cat">
<TextBlock FontSize="32"
Text="🐈" />
</controls:Case>
<controls:Case Value="Dog">
<TextBlock FontSize="32"
Text="🐕" />
</controls:Case>
<controls:Case Value="Bunny">
<TextBlock FontSize="32"
Text="🐇" />
</controls:Case>
<controls:Case Value="Llama">
<TextBlock FontSize="32"
Text="🦙" />
</controls:Case>
<controls:Case Value="Parrot">
<TextBlock FontSize="32"
Text="🦜" />
</controls:Case>
<controls:Case Value="Squirrel">
<TextBlock FontSize="32"
Text="🐿" />
</controls:Case>
</controls:SwitchPresenter>
</StackPanel>
</Page>
Sample // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace PrimitivesExperiment.Samples.SwitchPresenter;
[ToolkitSample(id: nameof(SwitchPresenterValueSample), "SwitchPresenter Value", description: $"A sample for showing how to use a {nameof(SwitchPresenter)} for state changes from an enum.")]
public sealed partial class SwitchPresenterValueSample : Page
{
public SwitchPresenterValueSample()
{
this.InitializeComponent();
}
public ObservableCollection<Animal> Items
{
get { return (ObservableCollection<Animal>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register(nameof(Items), typeof(ObservableCollection<Animal>), typeof(SwitchPresenterValueSample), new PropertyMetadata(new ObservableCollection<Animal>(Enum.GetValues<Animal>())));
}
public enum Animal
{
Cat,
Dog,
Bunny,
Llama,
Parrot,
Squirrel
} To which we get this error and stack trace:
I'm not sure what to make of this. The problem is in binding the ComboBox, not in the SwitchPresenter or any toolkit helper. @Sergio0694 do you have any ideas? 🤔 |
That's weird, cc. @manodasanW |
@Sergio0694 Can confirm, this fixes the issue. |
Though this sample is still broken with NAOT on WinUI 3... :( Tried a couple of the suggested workarounds, but they did not work on my machine, see #516
Though this sample is still broken with NAOT on WinUI 3... :( Tried a couple of the suggested workarounds, but they did not work on my machine, see #516
…eredLayout in WinUI 3 We were hitting issues with ItemSource setting in WinUI 3, per similar notes in CommunityToolkit/Windows#516 Updating CsWin32 provided better error messages (mostly to do with using {Binding} still in samples), but this appears to resolve the ItemsSource issue I was seeing before
…eredLayout in WinUI 3 We were hitting issues with ItemSource setting in WinUI 3, per similar notes in CommunityToolkit/Windows#516 Updating CsWin32 provided better error messages (mostly to do with using {Binding} still in samples), but this appears to resolve the ItemsSource issue I was seeing before
…eredLayout in WinUI 3 We were hitting issues with ItemSource setting in WinUI 3, per similar notes in CommunityToolkit/Windows#516 Updating CsWin32 provided better error messages (mostly to do with using {Binding} still in samples), but this appears to resolve the ItemsSource issue I was seeing before
Describe the bug
When running the gallery with the Wasdk head on the latest
main
commit, the 'SwitchPresenter' sample will crash the app:Exception message:
Stack trace:
This behavior is not present when running the gallery under UWP.
Steps to reproduce
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.
The text was updated successfully, but these errors were encountered: