microsoft-ui-xaml: Conditional XAML not working in ResourceDictionaries

Describe the bug Been having trouble trying to add more complex conditional XAML to our Resource Dictionaries (like just copying resources from the WinUI repo here that is using the same syntax is failing in our C# UWP Class Library).

The latest message I’ve received is Type 'TemplateBinding' used after '{' must be a Markup Extension. Error code 0x09cc. which seems similar to this old thread here.

Steps to reproduce the bug

  1. Clone WCT repo
  2. Open a ResourceDictionary for a control like (TokenizingTextbox.AutoSuggestBox.xaml)
  3. Paste in a system style for AutoSuggestBox (grabbed from Generic.xaml from 18362 for instance)
  4. Use VS Quick-Fix tip to apply conditional XAML for Description property.
  5. Try and compile. This should be valid XAML, but it fails.

I noticed this as well when trying to apply CornerRadius conditionally as well.

Expected behavior Compile and run.

Screenshots image

Version Info Class Library Min: 16299, Target 17763

Windows 10 version Saw the problem?
May 2020 Update (19041) Yes
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Mobile
Xbox
Surface Hub
IoT

VS 2019 16.5.5

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 10
  • Comments: 17 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Hitting this again with the Expander setter:

https://github.com/microsoft/microsoft-ui-xaml/blob/f132a034af64f83c65a2a20590c39399558905a4/dev/Expander/Expander.xaml#L6-L24

1>  Target MarkupCompilePass1:
1>    (Out) GeneratedCodeFiles: 'H:\code\Labs-UWP\labs\Expander\src\obj\Debug\uap10.0.17763\XamlTypeInfo.g.cs'
1>    (Out) GeneratedXamlFiles: 'H:\code\Labs-UWP\labs\Expander\src\obj\Debug\uap10.0.17763\Expander.xaml;H:\code\Labs-UWP\labs\Expander\src\obj\Debug\uap10.0.17763\Themes\Generic.xaml'
1>    (Out) GeneratedXbfFiles: 'H:\code\Labs-UWP\labs\Expander\src\obj\Debug\uap10.0.17763\Expander.xbf;H:\code\Labs-UWP\labs\Expander\src\obj\Debug\uap10.0.17763\Themes\Generic.xbf'
1>    (Out) Compile: 'AdditionalAssemblyInfo.cs;Expander.cs;H:\code\Labs-UWP\labs\Expander\src\obj\Debug\uap10.0.17763\XamlTypeInfo.g.cs'
1>  Target XamlPreCompile:
1>    CompilerServer: server - server processed compilation - aaa7992b-2225-4d8b-945c-a49f1c28c039
1>  Target MarkupCompilePass2:
1>    Expander.xaml(23,62): XamlCompiler error WMC0610: The XAML Binary Format (XBF) generator reported syntax error '0x03e9'
1>  Done building target "MarkupCompilePass2" in project "CommunityToolkit.Labs.WinUI.Expander.csproj" -- FAILED.
1>
1>  Done building project "CommunityToolkit.Labs.WinUI.Expander.csproj" -- FAILED.
1>  Target MarkupCompilePass1:
1>    (Out) GeneratedCodeFiles: 'H:\code\Labs-UWP\labs\Expander\src\obj\Debug\net6.0-windows10.0.19041.0\XamlTypeInfo.g.cs'
1>    (Out) GeneratedXamlFiles: 'H:\code\Labs-UWP\labs\Expander\src\obj\Debug\net6.0-windows10.0.19041.0\Expander.xaml;H:\code\Labs-UWP\labs\Expander\src\obj\Debug\net6.0-windows10.0.19041.0\Themes\Generic.xaml'
1>    (Out) GeneratedXbfFiles: 'H:\code\Labs-UWP\labs\Expander\src\obj\Debug\net6.0-windows10.0.19041.0\Expander.xbf;H:\code\Labs-UWP\labs\Expander\src\obj\Debug\net6.0-windows10.0.19041.0\Themes\Generic.xbf'
1>    (Out) Compile: 'AdditionalAssemblyInfo.cs;Expander.cs;H:\code\Labs-UWP\labs\Expander\src\obj\Debug\net6.0-windows10.0.19041.0\XamlTypeInfo.g.cs'
1>  Target XamlPreCompile:
1>    CompilerServer: server - server processed compilation - c836179b-62cb-4002-9de1-19f45a2f9efd
1>  Target MarkupCompilePass2:
1>    Expander.xaml(23,62): XamlCompiler error WMC0610: The XAML Binary Format (XBF) generator reported syntax error '0x03e9'

Looks like it is coming from both our UWP and WinAppSDK passes. The error message is also less helpful now than it was before.

I just copied the style into a new C# class library (in our Toolkit Labs) as I’m trying to investigate inheriting from the WinUI Expander for the Toolkit for 8.0. (Though since our min target is 17763, I think I can remove these checks, but still very confused why this is working in some places but not others still.)

My understanding is that WinUI 2 has a custom build task that strips out Conditional XAML predicates and outputs version-specific generic.xaml/themeresources.xaml for each supported OS version. That’s why you’re unable to just copy/paste styles from WinUI 2’s source code but WinUI 2 runs without issue.

Working around this limitation is tricky if you’re trying to do so with default styles. You might have success by conditionally declaring all necessary variants of the style as explicit resources and then in the default Style conditionally point its BasedOn property to each of those variants.

@evelynwu-msft Will this fix be coming to UWP/WinUI 2 or is there a fix/workaround for that you’re aware of?

Conditional XAML can be freely used within Styles when Windows App SDK 1.7 releases.

This problem persists even when targeting 19041 (min 17134). image

It only affects Setters in my case. I use contract7Present:CornerRadius and contract7Present:Description in other places in this file and it compiles just fine.

VS 2019 16.8.3