如果Windows 10使用黑暗主题(UWP),请设置TextBlock文本

时间:2016-09-21 16:12:18

标签: c# visual-studio xaml uwp windows-10-universal

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

RequestedTheme == ElementTheme.Dark

但它没有用。

编辑: 我想像这样设置它

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

3 个答案:

答案 0 :(得分:1)

ApplicationThemeEnum,您可以查看对您的应用程序强加的主题。您可以在下面查看。

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

更多信息Here

答案 1 :(得分:0)

您可以尝试:

Application.Current.Resources["SystemAccentColor"]

答案 2 :(得分:0)

您可以使用{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>