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

7rfyedvj  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(133)

我将默认的WinUI按钮样式从WinUI GitHub repo复制到一个空项目中。下面是我的App.xaml。当我尝试运行该项目时,我得到这个错误:XAML二进制格式(XBF)生成器报告语法错误“0x03e9”。如果删除条件setter,它将运行。控件模板内的条件XAML工作正常。哪里出错了?

<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>
4si2a6ki

4si2a6ki1#

我猜您的问题与这个已知的issue有关,它基本上说您不能将SettersConditional XAML一起使用(目前?)。
您可以找到一个变通方法here。首先创建一个基本样式,然后使用Conditional XAML创建样式。

<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>

顺便说一句,我注解掉了这一行,

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

因为在我示例应用程序中找不到ControlCotnerRadius

相关问题