Шаблон стиля элемента управления Button и свойство BackgroundSizing - PullRequest
0 голосов
/ 29 ноября 2018

У меня возникли некоторые проблемы с шаблоном стиля кнопки.
Я пытался изменить фон кнопки при наведении мыши на кнопку.
Когда я создал для нее таблицу стилей, щелкнув правой кнопкой мыши в средстве просмотра дизайна xaml - нажмите кнопку "Копировать".шаблон.

успешно, я получил шаблон элемента управления «Словарь ресурсов» Button Control.
Но я получил сообщение об ошибке при компиляции.Я мог бы скомпилировать его без определения таблицы стилей.Я мог бы найти сообщение об ошибке со свойством 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---->

enter image description here

Первая - это другая проблема, а 2-я, 3-я, 4-я, 5-я - проблема, связанная с шаблоном управления кнопками.

Я удалил свойство BackgroundSizing.тогда я не получаю сообщение об ошибке, но приложение останавливается.поэтому, когда я запускаю приложение в режиме отладки, я могу найти эту ошибку.

enter image description here

, когда я установил целевую версию 1809, тогда все проблемы решены ....

Но я слышал, что некоторые устройства должныЦелевая нижняя версия (?) может быть ..

Как я могу решить ошибку свойства BackgroundSizing?

1 Ответ

0 голосов
/ 29 ноября 2018

BackgroundSizing фактически был представлен в Windows 10 версии 1809 (введен v10.0.17763.0), поэтому он работает над сборкой 1809, а не с версией сборки ниже, чем она.Вы можете обратиться к этой MSDN документации.

Чтобы решить эту проблему, вы должны использовать условный Xaml.

Вы можете создать такой стиль:

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

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

И теперь вы можете использовать этот стиль, как показано ниже:

<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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...