xamluwpstyleswinui-3winui

Why Are Conditional XAML Setters In Default WinUI Styles Invalid In UWP?


I copied the default WinUI button style from the WinUI GitHub repo into an empty project. Below is my App.xaml. When I try to run the project, I get this error: The XAML Binary Format (XBF) generator reported syntax error '0x03e9'. If I remove the conditional setters, it runs. The conditional XAML within the control template works fine. What am I doing wrong?

<Application
x:Class="App1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:contract7Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,7)"
xmlns:contract7NotPresent="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractNotPresent(Windows.Foundation.UniversalApiContract,7)"
xmlns:local="using:Microsoft.UI.Xaml.Controls"
>

<Application.Resources>
    <Style x:Key="DefaultButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="{ThemeResource ButtonBackground}" />
        <contract7Present:Setter Property="BackgroundSizing" Value="InnerBorderEdge" />
        <Setter Property="Foreground" Value="{ThemeResource ButtonForeground}" />
        <Setter Property="BorderBrush" Value="{ThemeResource ButtonBorderBrush}" />
        <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}" />
        <contract7NotPresent:Setter Property="Padding" Value="11,5,11,5" />
        <contract7Present:Setter Property="Padding" Value="{StaticResource ButtonPadding}" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
        <Setter Property="FontWeight" Value="Normal" />
        <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
        <Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}" />
        <Setter Property="FocusVisualMargin" Value="-3" />
        <contract7Present:Setter Property="CornerRadius" Value="{ThemeResource ControlCornerRadius}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <ContentPresenter
                    x:Name="ContentPresenter"
                    Background="{TemplateBinding Background}"
                    contract7Present:BackgroundSizing="{TemplateBinding BackgroundSizing}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    Content="{TemplateBinding Content}"
                    ContentTemplate="{TemplateBinding ContentTemplate}"
                    ContentTransitions="{TemplateBinding ContentTransitions}"
                    contract7Present:CornerRadius="{TemplateBinding CornerRadius}"
                    contract7NotPresent:CornerRadius="{ThemeResource ControlCornerRadius}"
                    Padding="{TemplateBinding Padding}"
                    HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                    VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                    AutomationProperties.AccessibilityView="Raw"
                    local:AnimatedIcon.State="Normal">

                        <contract7Present:ContentPresenter.BackgroundTransition>
                            <contract7Present:BrushTransition Duration="0:0:0.083" />
                        </contract7Present:ContentPresenter.BackgroundTransition>

                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>

                                <VisualState x:Name="PointerOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundPointerOver}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPointerOver}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPointerOver}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                    <VisualState.Setters>
                                        <Setter Target="ContentPresenter.(local:AnimatedIcon.State)" Value="PointerOver"/>
                                    </VisualState.Setters>
                                </VisualState>

                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundPressed}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPressed}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPressed}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                    <VisualState.Setters>
                                        <Setter Target="ContentPresenter.(local:AnimatedIcon.State)" Value="Pressed"/>
                                    </VisualState.Setters>
                                </VisualState>

                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundDisabled}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushDisabled}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundDisabled}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                    <VisualState.Setters>
                                        <!-- DisabledVisual Should be handled by the control, not the animated icon. -->
                                        <Setter Target="ContentPresenter.(local:AnimatedIcon.State)" Value="Normal"/>
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </ContentPresenter>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

Solution

  • I guess your problem is related to this known issue that basically says that you can't use Setters with Conditional XAML (for the moment?).

    You can find a workaround here. First create a base style, then create styles using Conditional XAML. Something like this.

    <Application
        x:Class="App1.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:contract7NotPresent="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractNotPresent(Windows.Foundation.UniversalApiContract,7)"
        xmlns:contract7Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,7)"
        xmlns:local="using:Microsoft.UI.Xaml.Controls">
    
        <Application.Resources>
            <Style
                x:Key="DefaultButtonBaseStyle"
                TargetType="Button">
                <Setter Property="Background" Value="{ThemeResource ButtonBackground}" />
                <!--<contract7Present:Setter
                    Property="BackgroundSizing"
                    Value="InnerBorderEdge" />-->
                <Setter Property="Foreground" Value="{ThemeResource ButtonForeground}" />
                <Setter Property="BorderBrush" Value="{ThemeResource ButtonBorderBrush}" />
                <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}" />
                <!--<contract7NotPresent:Setter
                    Property="Padding"
                    Value="11,5,11,5" />-->
                <!--<contract7Present:Setter
                    Property="Padding"
                    Value="{StaticResource ButtonPadding}" />-->
                <Setter Property="HorizontalAlignment" Value="Left" />
                <Setter Property="VerticalAlignment" Value="Center" />
                <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
                <Setter Property="FontWeight" Value="Normal" />
                <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
                <Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}" />
                <Setter Property="FocusVisualMargin" Value="-3" />
                <!--<contract7Present:Setter
                    Property="CornerRadius"
                    Value="{ThemeResource ControlCornerRadius}" />-->
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <ContentPresenter
                                x:Name="ContentPresenter"
                                Padding="{TemplateBinding Padding}"
                                HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                contract7NotPresent:CornerRadius="{ThemeResource ControlCornerRadius}"
                                contract7Present:BackgroundSizing="{TemplateBinding BackgroundSizing}"
                                contract7Present:CornerRadius="{TemplateBinding CornerRadius}"
                                local:AnimatedIcon.State="Normal"
                                AutomationProperties.AccessibilityView="Raw"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Content="{TemplateBinding Content}"
                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                ContentTransitions="{TemplateBinding ContentTransitions}">
                                <contract7Present:ContentPresenter.BackgroundTransition>
                                    <contract7Present:BrushTransition Duration="0:0:0.083" />
                                </contract7Present:ContentPresenter.BackgroundTransition>
    
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal" />
    
                                        <VisualState x:Name="PointerOver">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames
                                                    Storyboard.TargetName="ContentPresenter"
                                                    Storyboard.TargetProperty="Background">
                                                    <DiscreteObjectKeyFrame
                                                        KeyTime="0"
                                                        Value="{ThemeResource ButtonBackgroundPointerOver}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames
                                                    Storyboard.TargetName="ContentPresenter"
                                                    Storyboard.TargetProperty="BorderBrush">
                                                    <DiscreteObjectKeyFrame
                                                        KeyTime="0"
                                                        Value="{ThemeResource ButtonBorderBrushPointerOver}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames
                                                    Storyboard.TargetName="ContentPresenter"
                                                    Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame
                                                        KeyTime="0"
                                                        Value="{ThemeResource ButtonForegroundPointerOver}" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                            <VisualState.Setters>
                                                <Setter Target="ContentPresenter.(local:AnimatedIcon.State)" Value="PointerOver" />
                                            </VisualState.Setters>
                                        </VisualState>
    
                                        <VisualState x:Name="Pressed">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames
                                                    Storyboard.TargetName="ContentPresenter"
                                                    Storyboard.TargetProperty="Background">
                                                    <DiscreteObjectKeyFrame
                                                        KeyTime="0"
                                                        Value="{ThemeResource ButtonBackgroundPressed}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames
                                                    Storyboard.TargetName="ContentPresenter"
                                                    Storyboard.TargetProperty="BorderBrush">
                                                    <DiscreteObjectKeyFrame
                                                        KeyTime="0"
                                                        Value="{ThemeResource ButtonBorderBrushPressed}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames
                                                    Storyboard.TargetName="ContentPresenter"
                                                    Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame
                                                        KeyTime="0"
                                                        Value="{ThemeResource ButtonForegroundPressed}" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                            <VisualState.Setters>
                                                <Setter Target="ContentPresenter.(local:AnimatedIcon.State)" Value="Pressed" />
                                            </VisualState.Setters>
                                        </VisualState>
    
                                        <VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames
                                                    Storyboard.TargetName="ContentPresenter"
                                                    Storyboard.TargetProperty="Background">
                                                    <DiscreteObjectKeyFrame
                                                        KeyTime="0"
                                                        Value="{ThemeResource ButtonBackgroundDisabled}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames
                                                    Storyboard.TargetName="ContentPresenter"
                                                    Storyboard.TargetProperty="BorderBrush">
                                                    <DiscreteObjectKeyFrame
                                                        KeyTime="0"
                                                        Value="{ThemeResource ButtonBorderBrushDisabled}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames
                                                    Storyboard.TargetName="ContentPresenter"
                                                    Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame
                                                        KeyTime="0"
                                                        Value="{ThemeResource ButtonForegroundDisabled}" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                            <VisualState.Setters>
                                                <!--  DisabledVisual Should be handled by the control, not the animated icon.  -->
                                                <Setter Target="ContentPresenter.(local:AnimatedIcon.State)" Value="Normal" />
                                            </VisualState.Setters>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                            </ContentPresenter>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    
            <contract7Present:Style
                x:Key="DefaultButtonStyle"
                BasedOn="{StaticResource DefaultButtonBaseStyle}"
                TargetType="Button">
                <Setter Property="BackgroundSizing" Value="InnerBorderEdge" />
                <Setter Property="Padding" Value="{StaticResource ButtonPadding}" />
                <!--<Setter Property="CornerRadius" Value="{ThemeResource ControlCornerRadius}" />-->
            </contract7Present:Style>
    
            <contract7NotPresent:Style
                x:Key="DefaultButtonStyle"
                BasedOn="{StaticResource DefaultButtonBaseStyle}"
                TargetType="Button">
                <Setter Property="Padding" Value="11,5,11,5" />
            </contract7NotPresent:Style>
    
        </Application.Resources>
    
    </Application>
    

    BTW, I commented out this line,

    <!--<Setter Property="CornerRadius" Value="{ThemeResource ControlCornerRadius}" />-->
    

    since ControlCotnerRadius couldn't be found in my sample app.