"error: Unable to use event X without a backing class (use x:Class)" in a DataTemplate in a ResourceDictionary #14286
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,...)
Current behavior
In my project, I am referring to a
DataTemplate
in a separateResourceDictionary
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:With
MyContent
defined inMainPage.XAML.cs
:MyDataTemplate
defined inResourceDictionaryWithCodeBehind1.XAML
:And with the code-behind the
ResourceDictionary
including:The compilation errors are:
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 inMyDataType
.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)
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.
The text was updated successfully, but these errors were encountered: