如果 Windows 10 使用深色主题 (UWP),则设置 TextBlock 文本

Set TextBlock Text if Windows 10 uses dark theme (UWP)

如果 windows 10 用户使用 dark/light 主题,我想设置 TextBlock 的文本。我试过了

RequestedTheme == ElementTheme.Dark

但是没用。

编辑: 我想这样设置

if(user uses dark them)
{
    mTextBlock.Text = "Dark"
}
elseif(user uses light theme)
{
   mTextBlock.Text = "Light"
}

你可以试试:

Application.Current.Resources["SystemAccentColor"]

ApplicationTheme 是一个 Enum 用于检查您的应用程序强加的主题。您可以像下面这样检查。

if (Application.Current.RequestedTheme == ApplicationTheme.Dark)
{
    mTextBlock.Text = "Dark"
}
elseif(Application.Current.RequestedTheme == ApplicationTheme.Light)
{
   mTextBlock.Text = "Light"
}

更多信息Here

您可以使用{ThemeResource} 标记扩展来实现您想要做的事情。 在你的 Page.xaml:

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>                
            <ResourceDictionary Source="Dictionary2.xaml" x:Key="Dark"/>
            <ResourceDictionary Source="Dictionary1.xaml" x:Key="Light"/>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Page.Resources>
<StackPanel>
    <TextBlock Text="{ThemeResource txt}"/>
</StackPanel>

Dictionary1.xaml:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Styles">

<x:String x:Key="txt">Light</x:String>

</ResourceDictionary>

Dictionary2.xaml:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Styles">

<x:String x:Key="txt">Dark</x:String>

</ResourceDictionary>