简体   繁体   English

按钮控件样式模板和BackgroundSizing属性

[英]Button control style template and BackgroundSizing Property

I have some problem with Button Style template. 我对“按钮样式”模板有一些问题。
I tried to change button background when mouse hover on button. 当鼠标悬停在按钮上时,我尝试更改按钮背景。
When I created style sheet for it by right-clicking mouse on xaml design viewer - click on copy template. 当我通过在XAML设计查看器上单击鼠标右键为其创建样式表时,请单击复制模板。

successfully, I got a Control Template "Resource Dictionary" of Button Control. 成功地,我获得了按钮控件的控件模板“资源字典”。
But I got a error message when I compiled it. 但是编译时收到错误消息。 I could compile it without definition of style sheet. 我可以在不定义样式表的情况下进行编译。 I could find a error message with a BackgroundSizing Property. 我可以找到带有BackgroundSizing属性的错误消息。

<Style x:Key="ButtonStyle1" TargetType="Button">
    <Setter Property="Background" Value="{ThemeResource ButtonBackground}"/>
    <Setter Property="BackgroundSizing" Value="OuterBorderEdge"/>
    <Setter Property="Foreground" Value="{ThemeResource ButtonForeground}"/>
    <Setter Property="BorderBrush" Value="{ThemeResource ButtonBorderBrush}"/>
    <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}"/>
    <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"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" BackgroundSizing="{TemplateBinding BackgroundSizing}" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" CornerRadius="{TemplateBinding CornerRadius}" ContentTransitions="{TemplateBinding ContentTransitions}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
                    <VisualStateManager.VisualStateGroups> 
<---omitted---->

在此处输入图片说明

First one is another problem, and 2nd, 3rd, 4th, 5th are the problem relative with button control template. 第一个是另一个问题,第二,第三,第四,第五是与按钮控制模板有关的问题。

I deleted a BackgroundSizing property. 我删除了BackgroundSizing属性。 then I don't get any error message, but app stop. 那么我没有收到任何错误消息,但是应用程序停止了。 so when I run app with debug mode, I cound find this error. 因此,当我以调试模式运行应用程序时,我发现此错误。

在此处输入图片说明

when I set Target version 1809, then all problem solved.... 当我设置目标版本1809时,所有问题都解决了。

But I heard that some device needs to target lower version(?) maybe.. 但是我听说某些设备可能需要定位较低的版本(?)。

How can I solve BackgroundSizing Property error? 如何解决BackgroundSizing属性错误?

BackgroundSizing was actually introduced in Windows 10, version 1809 (introduced v10.0.17763.0), so that's why it's working on 1809 build and not on the build version lower than it. 实际上, BackgroundSizing是在Windows 10版本1809(引入的v10.0.17763.0)中引入的,因此这就是为什么它可以在1809版本上运行,而不是在低于其版本的版本上运行的原因。 You can refer to this MSDN documentation. 您可以参考此MSDN文档。

In order to resolve this issue, you have to use conditional Xaml. 为了解决此问题,您必须使用条件Xaml。

You can create a style like this: 您可以创建如下样式:

BaseButtonStyle BaseButtonStyle

 <Style x:Key="BaseButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="{ThemeResource ButtonBackground}"/>
        <Setter Property="Foreground" Value="{ThemeResource ButtonForeground}"/>
        <Setter Property="BorderBrush" Value="{ThemeResource ButtonBorderBrush}"/>
        <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}"/>
        <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"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" BackgroundSizing="{TemplateBinding BackgroundSizing}" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" CornerRadius="{TemplateBinding CornerRadius}" ContentTransitions="{TemplateBinding ContentTransitions}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
                        <VisualStateManager.VisualStateGroups> 
<---omitted---->

ButtonStyle1 ButtonStyle1

 <Style x:Key="ButtonStyle1" TargetType="Button" BasedOn={StaticResource BaseButtonStyle}>
    <Setter Property="BackgroundSizing" Value="OuterBorderEdge"/>
 </Style>

And now you can consume this style as below: 现在您可以使用以下样式:

<Page
    x:Class="ConditionalTest.MainPage"
    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)">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button contract7Present:Style="{StaticResource ButtonStyle1}"
                contract7NotPresent:Style="{StaticResource BaseButtonStyle}"/>
    </Grid>
</Page>

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

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