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

WinUI 3 in desktop: XAML parsing failed #5714

Closed
yv73 opened this issue Aug 18, 2021 · 9 comments
Closed

WinUI 3 in desktop: XAML parsing failed #5714

yv73 opened this issue Aug 18, 2021 · 9 comments

Comments

@yv73
Copy link

yv73 commented Aug 18, 2021

Windows 10 Pro 10.0.19043
Microsoft Visual Studio Community 2019 Version 16.11.1

Steps to reproduce:

File, New, App (WinUI 3 in desktop)
WTS.ProjectConfig.xml:

<xml
    xmlns:genTemplate="http://schemas.microsoft.com/appx/developer/windowsTemplateStudio">

    <genTemplate:Metadata>
        <genTemplate:Item Name="generator" Value="Windows Template Studio"/>
        <genTemplate:Item Name="wizardVersion" Version="v4.1.21179.1" />
        <genTemplate:Item Name="templatesVersion" Version="v4.1.21179.1" />
        <genTemplate:Item Name="projectType" Value="BlankAdvanced" />
        <genTemplate:Item Name="framework" Value="MVVMToolkit" />
        <genTemplate:Item Name="platform" Value="WinUI" />
        <genTemplate:Item Name="appmodel" Value="Desktop" />
    </genTemplate:Metadata>
</xml>

Add, New Item ... Resource Dictionary (WinUI 3) TextBox.xaml
Make the modifications to TextBox.xaml:

   <Style x:Key="TextBoxBaseStyle" TargetType="TextBox">
        <Setter Property="FontSize" Value="{StaticResource LargeFontSize}"/>
    </Style>

LargeFontSize is from /Styles/_FontSizes.xaml:

<x:Double x:Key="LargeFontSize">24</x:Double>

Make the modifications to App.xaml:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
                <ResourceDictionary Source="/Styles/_FontSizes.xaml" />
                <ResourceDictionary Source="/Styles/_Thickness.xaml" />
                <ResourceDictionary Source="/Styles/Page.xaml" />
                <ResourceDictionary Source="/Styles/TextBlock.xaml" />
                **<ResourceDictionary Source="/Styles/TextBox.xaml" />**
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

Build / execute
Observe exception at runtime: 'XAML parsing failed.'
"Cannot find a Resource with the Name/Key LargeFontSize [Line: 13 Position: 37]"

Is there something wrong?

@yv73 yv73 added the question label Aug 18, 2021
@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Aug 18, 2021
@StephenLPeters
Copy link
Contributor

@yv73 does the _FontSizes.xaml file contain only one resource, LargeFontSize? If so can you try adding a second resource to that dictionary and see if the issue still occurs? there is an optimization for resource dictionaries with only one entry which has bitten us in the past, I wonder if that is causing this.

@StephenLPeters StephenLPeters added needs-author-feedback Asked author to supply more information. and removed needs-triage Issue needs to be triaged by the area owners labels Aug 20, 2021
@yv73
Copy link
Author

yv73 commented Aug 20, 2021

_FontSizes.xaml is generated with Windows Template Studio (App (WinUI 3 in Desktop)):

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <x:Double x:Key="LargeFontSize">24</x:Double>

    <x:Double x:Key="MediumFontSize">16</x:Double>

</ResourceDictionary>

To set TextBox.xaml:

    <Style x:Key="TextBoxBaseStyle" TargetType="TextBox">
        <Setter Property="FontSize" Value="{StaticResource MediumFontSize}"/>
    </Style>

Observe exception at runtime: 'XAML parsing failed.'
"Cannot find a Resource with the Name/Key MediumFontSize [Line: 13 Position: 37]"

No problem if I use

    <Style x:Key="TextBoxBaseStyle" TargetType="TextBox">
        <Setter Property="FontSize" Value="{StaticResource TextStyleLargeFontSize}"/>
    </Style>

TextStyleLargeFontSize is from .nuget\packages\microsoft.projectreunion.winui\0.8.2\lib\net5.0-windows10.0.18362.0\Microsoft.WinUI\Themes\generic.xaml

<x:Double x:Key="TextStyleLargeFontSize">18.14</x:Double>

And LargeFontSize is used in TextBlock.xaml (generated with WTS) without problem:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!--Common texts-->
    <Style x:Key="PageTitleStyle" TargetType="TextBlock">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="FontWeight" Value="SemiLight" />
        <Setter Property="FontSize" Value="{StaticResource LargeFontSize}" />
        <Setter Property="TextTrimming" Value="CharacterEllipsis" />
        <Setter Property="TextWrapping" Value="NoWrap" />
    </Style>

    <Style x:Key="BodyTextStyle" TargetType="TextBlock">
        <Setter Property="FontWeight" Value="Normal" />
        <Setter Property="FontSize" Value="{StaticResource MediumFontSize}" />
        <Setter Property="TextTrimming" Value="CharacterEllipsis" />
        <Setter Property="TextWrapping" Value="Wrap" />
    </Style>

</ResourceDictionary>

Using LargeFontSize directly in MainPage.xaml:

<TextBox Text="TextBox" FontSize="{StaticResource LargeFontSize}"/>

is no problem too.

@ghost ghost added needs-triage Issue needs to be triaged by the area owners and removed needs-author-feedback Asked author to supply more information. labels Aug 20, 2021
@mrlacey
Copy link
Contributor

mrlacey commented Aug 20, 2021

@yv73 I can't reproduce the issue based on the step you've provided. Following them everything works fine for me.

Based on other information above, I suspect there may be more going on than is documented. Because:

  • The error message reports the error at line 13, but unless you've got a lot of empty lines, there's no need for TextBlox.xaml to have that many lines so I wonder if there's something else there that may be contributing to the problem.
  • WTS generates an app that references Project Reunion package version 0.8.0 but your error message references 0.8.2

If you have a minimal project that shows this, can you zip it up and share it?

@yv73
Copy link
Author

yv73 commented Aug 21, 2021

I updated Project Reunion package to version 0.8.2. With 0.8.0 same behavior.

App1.zip

2021-08-21

@yv73
Copy link
Author

yv73 commented Aug 26, 2021

Have you reproduced the issue?

@StephenLPeters StephenLPeters added area-XamlCompiler product-winui3 WinUI 3 issues team-Markup Issue for the Markup team labels Sep 14, 2021
@StephenLPeters
Copy link
Contributor

@RealTommyKlein and @fabiant3 FYI

@RealTommyKlein
Copy link
Contributor

I believe the TextBox.xaml and TextBlock.xaml dictionaries need to explicitly include _FontSize.xaml in their MergedDictionaries in order to resolve LargeFontSize and MediumFontSize. TextBox.xaml and TextBlock.xaml are being inserted into Application.Resources at the same time as _FontSize.xaml, so they wouldn't be able to resolve from _FontSize.xaml even though the Application.Resources.MergedDictionaries would suggest so. Making these changes worked for me:

TextBlock.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Styles/_FontSizes.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <!--Common texts-->
    <Style x:Key="PageTitleStyle" TargetType="TextBlock">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="FontWeight" Value="SemiLight" />
        <Setter Property="FontSize" Value="{StaticResource LargeFontSize}" />
        <Setter Property="TextTrimming" Value="CharacterEllipsis" />
        <Setter Property="TextWrapping" Value="NoWrap" />
    </Style>

    <Style x:Key="BodyTextStyle" TargetType="TextBlock">
        <Setter Property="FontWeight" Value="Normal" />
        <Setter Property="FontSize" Value="{StaticResource MediumFontSize}" />
        <Setter Property="TextTrimming" Value="CharacterEllipsis" />
        <Setter Property="TextWrapping" Value="Wrap" />
    </Style>

</ResourceDictionary>

TextBox.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1.Styles">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Styles/_FontSizes.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="TextBoxBaseStyle" TargetType="TextBox">
        <Setter Property="FontSize" Value="{StaticResource LargeFontSize}"/>
    </Style>

</ResourceDictionary>

@yv73
Copy link
Author

yv73 commented Sep 30, 2021

Yes, it works for me too. However, TextBlock.xaml works without explicitly including _FontSize.xaml.

@bpulliam bpulliam removed the needs-triage Issue needs to be triaged by the area owners label Dec 6, 2022
@github-actions
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants