5

我正在创建 Windows 应用商店应用程序。我使用 callisto 库在设置中创建弹出窗口。我对样式按钮有疑问。当我将鼠标悬停在背景上并且字体变为白色时...

看图(鼠标在第二个按钮上):番茄钟设置

这是我的 XAML 文件:

<UserControl
x:Class="Pomidoro.PomidoroUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Pomidoro"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Grid>
    <StackPanel x:Name="FlyoutContent">
        <Button 
            Name="ChoosePomidoroButton" 
            Click="ChoosePomidoroButton_Click"
            Content="Choose pomidoro image..."    
            Background="LightGray"
            Foreground="Black"
            BorderBrush="Black"           
            />
        <Button 
            Name="DefaultPomidoroButton" 
            Click="DefaultPomidoroButton_Click"
            Content="Set default pomidoro image"  
            Background="LightGray"
            Foreground="Black"
            BorderBrush="Black"
            />
    </StackPanel>
</Grid>

这就是我在 App.xaml.cs 中创建弹出窗口的方式:

            // Add an Pomidoro settings
        var pomidoro = new SettingsCommand("pomidoro", "Pomidoro image", (handler) =>
        {
            var settings = new SettingsFlyout();
            settings.Content = new PomidoroUserControl();
            settings.HeaderText = "Pomidoro";
            settings.IsOpen = true;
        });

        args.Request.ApplicationCommands.Add(pomidoro);

当我尝试使用默认样式时...

<Button 
            Name="DefaultPomidoroButton" 
            Click="DefaultPomidoroButton_Click"
            Content="Set default pomidoro image"
            />

...背景、边框和前景是白色的...用户看不到任何东西。

我应该怎么做才能应用默认样式以具有灰色按钮(就像在商店中的许多应用程序中一样)?

4

1 回答 1

2

问题是当您的应用程序使用深色主题时,默认按钮样式使用白色前景和边框画笔以及透明背景画笔。在页面上,默认按钮样式看起来很好:

深色背景下默认按钮样式的屏幕截图

但是,在 Callisto SettingsFlyout 的内容窗格中,该按钮是不可见的,因为内容窗格的背景是白色的。您一定已经注意到这一点,因为您在 UserControl 中手动设置了按钮的 Foreground 和 Background 属性。

一种解决方案是为 SettingsFlyout 上的按钮定义新样式,使用默认按钮模板来执行此操作。默认样式位于 64 位机器上:

C:\Program Files (x86)\Windows Kits\8.0\Include\WinRT\Xaml\Design

default.xaml我在此文件夹中找到了 Button 控件的默认样式。

首先,我将这个默认样式复制到一个新的资源字典中。我设置 App.xaml 来引用这个新的资源字典,如下所示:

<!-- Add this line to your MergedDictionaries in App.xaml -->
<ResourceDictionary Source="FlyoutResources.xaml"/>

通过一些工作,我调整了复制的默认按钮样式并给它一个独特的键。这是示例:

<!-- contents of FlyoutResources.xaml -->
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1">

    <ResourceDictionary.ThemeDictionaries>
        <ResourceDictionary x:Key="Default">
            <SolidColorBrush x:Key="FlyoutButtonForeground">#FF000000</SolidColorBrush>
            <SolidColorBrush x:Key="FlyoutButtonBackground">#FFD3D3D3</SolidColorBrush>
            <SolidColorBrush x:Key="FlyoutButtonBorder">#FF000000</SolidColorBrush>
            <SolidColorBrush x:Key="FlyoutButtonPointerOverBackgroundThemeBrush" Color="#21D3D3D3" />
            <SolidColorBrush x:Key="FlyoutButtonPointerOverForegroundThemeBrush" Color="#FF000000" />
            <SolidColorBrush x:Key="FlyoutButtonPressedBackgroundThemeBrush" Color="#FFFFFFFF" />
            <SolidColorBrush x:Key="FlyoutButtonPressedForegroundThemeBrush" Color="#FF000000" />
            <SolidColorBrush x:Key="FlyoutButtonDisabledBackgroundThemeBrush" Color="#FFD3D3D3" />
            <SolidColorBrush x:Key="FlyoutButtonDisabledBorderThemeBrush" Color="#66000000" />
            <SolidColorBrush x:Key="FlyoutButtonDisabledForegroundThemeBrush" Color="#66000000" />
        </ResourceDictionary>
    </ResourceDictionary.ThemeDictionaries>

    <Style TargetType="Button" x:Key="flyoutButton">
        <Setter Property="Background" Value="{StaticResource FlyoutButtonBackground}" />
        <Setter Property="Foreground" Value="{StaticResource FlyoutButtonForeground}"/>
        <Setter Property="BorderBrush" Value="{StaticResource FlyoutButtonBorder}" />
        <Setter Property="BorderThickness" Value="{StaticResource ButtonBorderThemeThickness}" />
        <Setter Property="Padding" Value="12,4,12,4" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}" />
        <Setter Property="FontWeight" Value="SemiBold" />
        <Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="PointerOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                                       Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource FlyoutButtonPointerOverBackgroundThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                       Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource FlyoutButtonPointerOverForegroundThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                                       Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource FlyoutButtonPressedBackgroundThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                       Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource FlyoutButtonPressedForegroundThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                                       Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource FlyoutButtonDisabledBackgroundThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                                       Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource FlyoutButtonDisabledBorderThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                       Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource FlyoutButtonDisabledForegroundThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="FocusVisualWhite"
                                                         Storyboard.TargetProperty="Opacity"
                                                         To="1"
                                                         Duration="0" />
                                        <DoubleAnimation Storyboard.TargetName="FocusVisualBlack"
                                                         Storyboard.TargetProperty="Opacity"
                                                         To="1"
                                                         Duration="0" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unfocused" />
                                <VisualState x:Name="PointerFocused" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="Border"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Margin="3">
                            <ContentPresenter x:Name="ContentPresenter"
                                              Content="{TemplateBinding Content}"
                                              ContentTransitions="{TemplateBinding ContentTransitions}"
                                              ContentTemplate="{TemplateBinding ContentTemplate}"
                                              Margin="{TemplateBinding Padding}"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        </Border>
                        <Rectangle x:Name="FocusVisualWhite"
                                   IsHitTestVisible="False"
                                   Stroke="{StaticResource FocusVisualWhiteStrokeThemeBrush}"
                                   StrokeEndLineCap="Square"
                                   StrokeDashArray="1,1"
                                   Opacity="0"
                                   StrokeDashOffset="1.5" />
                        <Rectangle x:Name="FocusVisualBlack"
                                   IsHitTestVisible="False"
                                   Stroke="{StaticResource FocusVisualBlackStrokeThemeBrush}"
                                   StrokeEndLineCap="Square"
                                   StrokeDashArray="1,1"
                                   Opacity="0"
                                   StrokeDashOffset="0.5" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

最后一步是在 XAML 按钮定义上设置该样式:

<Button 
    Name="ChoosePomidoroButton" 
    Content="Choose pomidoro image..."   
        Style="{StaticResource flyoutButton}"
    />

<!-- etc. -->

这是它的外观(中间按钮处于悬停状态):

自定义样式按钮的屏幕截图

于 2013-01-20T20:37:03.210 回答