Skip to content
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

"error: Unable to use event X without a backing class (use x:Class)" in a DataTemplate in a ResourceDictionary #14286

Closed
christianfo opened this issue Nov 5, 2023 · 3 comments · Fixed by #14737
Labels
area/code-generation Categorizes an issue or PR as relevant to code generation difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI kind/regression Something was working, now it isn't project/resources 🈷️ Categorizes an issue or PR as relevant to resources and localization (Resources, Assets,...)

Comments

@christianfo
Copy link

christianfo commented Nov 5, 2023

Current behavior

In my project, I am referring to a DataTemplate in a separate ResourceDictionary XAML file with code-behind. Binding to either a method in the code-behind or a method in the template's data context generates compilation error: "Unable to use event X without a backing class (use x:Class)". This reproes with Uno.WinUI 5.0.19 and appears to be a recent regression, as it is working fine for me with Uno.WinUI 4.10.26, same version of WinAppSDK (1.4.231008000). It also works fine in a regular WinAppSDK project.

I created a simple repro (attached and available at here on GitHub).

In MainPage.XAML I have:

  <Page.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <local:ResourceDictionaryWithCodeBehind1 />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Page.Resources>

  <StackPanel
    <ContentPresenter ContentTemplate="{StaticResource MyDataTemplate}" Content="{x:Bind MyContent}"/>
  </StackPanel>

With MyContent defined in MainPage.XAML.cs:

    public class MyDataType
    {
        public string Text { get; set; } = "Hello, world!";

        public void Switch_Toggled(object sender, RoutedEventArgs e)
        {
            (sender as ToggleSwitch).Header = "Switch toggled!";
        }
    }

    public sealed partial class MainPage : Page
    {
        ...
        public MyDataType MyContent { get; set; } = new MyDataType();
    }

MyDataTemplate defined in ResourceDictionaryWithCodeBehind1.XAML:

  <DataTemplate x:Key="MyDataTemplate" x:DataType="local:MyDataType">
    <StackPanel>
      <TextBlock Text="{x:Bind Text}" />
      <Button Margin="0,20,0,0" Click="Button_Click" >Click Me!</Button>
      <ToggleSwitch Margin="0,20,0,0" Header="Toggle me!" Toggled="{x:Bind Switch_Toggled}" />
    </StackPanel>
  </DataTemplate>

And with the code-behind the ResourceDictionary including:

       private void Button_Click(object sender, RoutedEventArgs e)
       {
           (sender as Button).Content = "Button clicked!";
       }

The compilation errors are:

Severity	Code	Description	Project	File	Line	Suppression State
Error	CS1029	#error: 'Unable to use event Click without a backing class (use x:Class)'	UnoApp1 (net7.0-windows10.0.19041)	C:\uno\UnoApp1\UnoApp1\Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\ResourceDictionaryWithCodeBehind1_f180aa87125b76be3a15ed45480f6552.cs	125	N/A
Error	CS1029	#error: 'Unable to use event Toggled without a backing class (use x:Class)'	UnoApp1 (net7.0-windows10.0.19041)	C:\uno\UnoApp1\UnoApp1\Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\ResourceDictionaryWithCodeBehind1_f180aa87125b76be3a15ed45480f6552.cs	143	N/A
Error	CS1029	#error: 'Unable to use event Click without a backing class (use x:Class)'	UnoApp1 (net7.0-windows10.0.19041)	C:\uno\UnoApp1\UnoApp1\Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\ResourceDictionaryWithCodeBehind1_f180aa87125b76be3a15ed45480f6552.cs	355	N/A
Error	CS1029	#error: 'Unable to use event Toggled without a backing class (use x:Class)'	UnoApp1 (net7.0-windows10.0.19041)	C:\uno\UnoApp1\UnoApp1\Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\ResourceDictionaryWithCodeBehind1_f180aa87125b76be3a15ed45480f6552.cs	373	N/A
Error	CS1029	#error: 'Unable to use event Click without a backing class (use x:Class)'	UnoApp1 (net7.0-windows10.0.19041)	C:\uno\UnoApp1\UnoApp1\Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\ResourceDictionaryWithCodeBehind1_f180aa87125b76be3a15ed45480f6552.cs	496	N/A
Error	CS1029	#error: 'Unable to use event Toggled without a backing class (use x:Class)'	UnoApp1 (net7.0-windows10.0.19041)	C:\uno\UnoApp1\UnoApp1\Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\ResourceDictionaryWithCodeBehind1_f180aa87125b76be3a15ed45480f6552.cs	514	N/A

Even though the errors appear to refer to the Windows build, this problem only happens when using UNO 5.0.19 and does not repro with UNO 4.10.26, with the same version of WinAppSDK (1.4.231008000).

Also, not only does binding to an event in the code-behind generate that error (which I understand to be questionable from issue 10109, binding to a method in the data context (with x:Bind) also generates the same error.

This appears to affect only events. Binding to properties in the data context appears to be working as demonstrating with the Text property in MyDataType.

Note that for the repro, I created the ResourceDictionary using the "ResourceDictionary with code-behind" template provided with the Uno Plaform.

Expected behavior

For the code to compile and for UNO 5.0.19 to behave as UNO 4.10.26 in that regard.

How to reproduce it (as minimally and precisely as possible)

Simple repro here: https://github.com/christianfo/Uno-ResourceDictionary-Issue

Workaround

It seems that I should be able to use a ContentControl instead of the ResourceDictionary. My use case is a bit more complicated that the simple repro in that it involves a TemplateSelector, but it appears that I can make the various DataTemplates that I need and the TemplateSelector resources of the ContentControl and successfully reference them. This works in a simple repro, now working on moving it to the real use case. Fingers crossed!

Works on UWP/WinUI

None

Environment

Uno.WinUI / Uno.WinUI.WebAssembly / Uno.WinUI.Skia, Uno.UI.DevServer / Uno.WinUI.DevServer

NuGet package version(s)

<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageVersion Include="Microsoft.Windows.Compatibility" Version="7.0.5" />
<PackageVersion Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.756" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.4.231008000" />
<PackageVersion Include="Uno.Core.Extensions.Logging.Singleton" Version="4.0.1" />
<PackageVersion Include="Uno.Extensions.Logging.OSLog" Version="1.7.0" />
<PackageVersion Include="Uno.Extensions.Logging.WebAssembly.Console" Version="1.7.0" />
<PackageVersion Include="Uno.Resizetizer" Version="1.2.0" />
<PackageVersion Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="5.0.19" />
<PackageVersion Include="Uno.UniversalImageLoader" Version="1.9.36" />
<PackageVersion Include="Uno.Wasm.Bootstrap" Version="8.0.0" />
<PackageVersion Include="Uno.Wasm.Bootstrap.DevServer" Version="8.0.0" />
<PackageVersion Include="Uno.Wasm.Bootstrap.Server" Version="8.0.0" />
<PackageVersion Include="Uno.WinUI" Version="5.0.19" />
<PackageVersion Include="Uno.WinUI.Lottie" Version="5.0.19" />
<PackageVersion Include="Uno.WinUI.DevServer" Version="5.0.19" />
<PackageVersion Include="Uno.WinUI.WebAssembly" Version="5.0.19" />
<PackageVersion Include="Xamarin.Google.Android.Material" Version="1.10.0.1" />

Affected platforms

Android, Windows

IDE

Visual Studio 2022

IDE version

17.7.6

Relevant plugins

No response

Anything else we need to know?

So far, I have only tested this with Windows and Android platforms.

@christianfo christianfo added difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification labels Nov 5, 2023
@Youssef1313 Youssef1313 added kind/regression Something was working, now it isn't area/code-generation Categorizes an issue or PR as relevant to code generation and removed kind/bug Something isn't working labels Nov 5, 2023
@MartinZikmund MartinZikmund added project/resources 🈷️ Categorizes an issue or PR as relevant to resources and localization (Resources, Assets,...) difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI and removed triage/untriaged Indicates an issue requires triaging or verification difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. labels Nov 6, 2023
@MartinZikmund
Copy link
Member

Possibly related to #12495, but surprising that it has worked in previous versions

@christianfo
Copy link
Author

In my test, it did. It also works on vanilla WinAppSDK. Though I can see how you could make an argument that firing an event to the code-behind is a bit weird in a ResourceDictionary (though pretty handy in my use case), I would expect x:Bind an event to a method of the data context to compile. x:Bind property binding to the data context does.

@sarosigy
Copy link

sarosigy commented Dec 7, 2023

It is related to #10094 and yes, it compiled before just gave a warning (// WARNING Unable to use event Click without a backing class (use x:Class) ) and the handlers never ever been called.

Anyway, thanks for solving this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/code-generation Categorizes an issue or PR as relevant to code generation difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI kind/regression Something was working, now it isn't project/resources 🈷️ Categorizes an issue or PR as relevant to resources and localization (Resources, Assets,...)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants