简体   繁体   English

为什么默认 WinUI Styles 中的条件 XAML 设置器在 UWP 中无效?

[英]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.我将默认的 WinUI 按钮样式从WinUI GitHub 存储库复制到一个空项目中。 Below is my App.xaml.下面是我的 App.xaml。 When I try to run the project, I get this error: The XAML Binary Format (XBF) generator reported syntax error '0x03e9'.当我尝试运行该项目时,出现此错误:XAML 二进制格式 (XBF) 生成器报告语法错误“0x03e9”。 If I remove the conditional setters, it runs.如果我删除条件设置器,它就会运行。 The conditional XAML within the control template works fine.控件模板中的条件 XAML 工作正常。 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>

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?).我猜你的问题与这个已知问题有关,该问题基本上说你不能将设置器与条件 XAML一起使用(暂时?)。

You can find a workaround here .您可以在此处找到解决方法。 First create a base style, then create styles using Conditional XAML .首先创建一个基本样式,然后使用条件 XAML 创建 styles 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.因为在我的示例应用程序中找不到ControlCotnerRadius

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM