【问题标题】:How to set a button's background reveal brush in .cs code?如何在 .cs 代码中设置按钮的背景显示画笔?
【发布时间】:2019-08-13 21:24:02
【问题描述】:

目前我知道如何使用一些系统画笔,例如

xxx.Background = (SolidColorBrush)Application.Current.Resources["SystemControlForegroundAccentBrush"];

但是如何使用显示画笔呢? 我也是这样用的,

xxx.Background = (SolidColorBrush)Application.Current.Resources["SystemControlBackgroundBaseLowRevealBackgroundBrush"];

它会抛出错误。

System.InvalidCastException:“Unable to cast object of type 
    'Microsoft.UI.Xaml.Media.RevealBackgroundBrush' to type 
    'Windows.UI.Xaml.Media.SolidColorBrush'.”

【问题讨论】:

    标签: uwp uwp-xaml


    【解决方案1】:

    我们不能像一般BackgroundBrush那样设置显示刷。 RevealBackgroundBrush 是动态效果,不独立存在。换句话说,RevealBackgroundBrush 取决于状态。要在自定义控件或重新模板化的控件上启用 Reveal,您需要修改控件的控件模板。大多数控件模板在根部都有一个网格;更新该根网格的VisualState 以使用 Reveal。例如,如果您想为按钮添加RevealBackgroundBrush

    <Style TargetType="Button" x:Key="ButtonStyle1">
        <Setter Property="Background" Value="{ThemeResource ButtonRevealBackground}" />
        <Setter Property="Foreground" Value="{ThemeResource ButtonForeground}" />
        <Setter Property="BorderBrush" Value="{ThemeResource ButtonRevealBorderBrush}" />
        <Setter Property="BorderThickness" Value="2" />
        <Setter Property="Padding" Value="8,4,8,4" />
        <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="True" />
        <Setter Property="FocusVisualMargin" Value="-3" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
    
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal">
    
                                    <Storyboard>
                                        <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                                    </Storyboard>
                                </VisualState>
    
                                <VisualState x:Name="PointerOver">
                                    <VisualState.Setters>
                                        <Setter Target="RootGrid.(RevealBrush.State)" Value="PointerOver" />
                                        <Setter Target="RootGrid.Background" Value="{ThemeResource ButtonRevealBackgroundPointerOver}" />
                                        <Setter Target="ContentPresenter.BorderBrush" Value="Transparent"/>
                                        <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource ButtonForegroundPointerOver}" />
                                    </VisualState.Setters>
    
                                    <Storyboard>
                                        <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                                    </Storyboard>
                                </VisualState>
    
                                <VisualState x:Name="Pressed">
                                    <VisualState.Setters>
                                        <Setter Target="RootGrid.(RevealBrush.State)" Value="Pressed" />
                                        <Setter Target="RootGrid.Background" Value="{ThemeResource ButtonRevealBackgroundPressed}" />
                                        <Setter Target="ContentPresenter.BorderBrush" Value="{ThemeResource ButtonRevealBackgroundPressed}" />
                                        <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource ButtonForegroundPressed}" />
                                    </VisualState.Setters>
    
                                    <Storyboard>
                                        <PointerDownThemeAnimation Storyboard.TargetName="RootGrid" />
                                    </Storyboard>
                                </VisualState>
    
                                <VisualState x:Name="Disabled">
                                    <VisualState.Setters>
                                        <Setter Target="RootGrid.Background" Value="{ThemeResource ButtonRevealBackgroundDisabled}" />
                                        <Setter Target="ContentPresenter.BorderBrush" Value="{ThemeResource ButtonRevealBorderBrushDisabled}" />
                                        <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource ButtonForegroundDisabled}" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
    
                        </VisualStateManager.VisualStateGroups>
                        <ContentPresenter x:Name="ContentPresenter"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}"
            Content="{TemplateBinding Content}"
            ContentTransitions="{TemplateBinding ContentTransitions}"
            ContentTemplate="{TemplateBinding ContentTemplate}"
            Padding="{TemplateBinding Padding}"
            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
            VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
            AutomationProperties.AccessibilityView="Raw" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style >
    

    用法

    Btn.Style = (Style)App.Current.Resources["ButtonStyle1"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多