Skip to content

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

@christianfo

Description

@christianfo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/code-generationCategorizes an issue or PR as relevant to code generationdifficulty/medium 🤔Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUIkind/regressionSomething was working, now it isn'tproject/resources 🈷️Categorizes an issue or PR as relevant to resources and localization (Resources, Assets,...)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions