本文介绍了如何根据主题选择风格颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要我所有的"删除"按钮有红色前景。所以我在我的App.xaml中创建了一个样式:

I want all of my "Delete" buttons to have red foreground. So I made a style in my App.xaml like that:

        < Style TargetType =" AppBarButton" x:Key =" PrzyciskUsun">

            < Setter Property =" Foreground" Value =" Red">< / Setter>

            < Setter Property =" Icon"值="删除">< / Setter>

            < Setter Property =" Label" Value =" Usun">< / Setter>

            < Setter Property =" ToolTipService.ToolTip" Value =" Del">< / Setter>

        < / Style>

        <Style TargetType="AppBarButton" x:Key="PrzyciskUsun">
            <Setter Property="Foreground" Value="Red"></Setter>
            <Setter Property="Icon" Value="Delete"></Setter>
            <Setter Property="Label" Value="Usun"></Setter>
            <Setter Property="ToolTipService.ToolTip" Value="Del"></Setter>
        </Style>

问题是在"黑暗"中主题红色应该更轻,在Light主题中红色应该更暗。所以我应该放置某种"IF"。在上面的风格。 IF Theme ="Dark"那么Value =" RoseRed"例如。
怎么做?

The problem is that in "Dark" theme the red should be lighter and in the Light theme the red should be darker. So I should place some kind of "IF" in style above. IF Theme = "Dark" then Value = "RoseRed" for example. How to do that?

推荐答案

也许最简单的方法是在代码中创建求助并使用它如下所示

Maybe the simplest way is to create recourse in code and use it like bellow

 class ColorResource
    {
        public static Color ThemeColor
        {
            get
            {
                if(App.Current.RequestedTheme == Windows.UI.Xaml.ApplicationTheme.Dark)
                {
                    return Colors.Red;
                }
                else
                {
                    return Colors.Green;
                }
            }
        }
    }

然后将其作为资源添加到App.xaml中

then add it as resource in App.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:local="using:App1"
    RequestedTheme="Light">
    <Application.Resources>
        <local:ColorResource x:Key="ThemColorResouce"/>
    </Application.Resources>
</Application>

并使用风格 

and use  it in style 

<Style x:Key="ButtonStyle1" TargetType="AppBarButton"> <Setter Property="Background" Value="{ThemeResource ButtonBackground}"/> <Setter Property="Foreground" Value="{Binding Source={StaticResource ThemColorResouce}, Path=ThemeColor}"/> <Setter Property="BorderBrush" Value="{ThemeResource ButtonBorderBrush}"/> <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}"/>

< Style />

<Style/>


这篇关于如何根据主题选择风格颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:26