2016-09-21 48 views
1

如果Windows 10用户使用深色/浅色主题,我想设置TextBlock的文本。我试过如果Windows 10使用深色主题(UWP),请设置TextBlock文本

RequestedTheme == ElementTheme.Dark 

但它不起作用。

编辑: 我想设置这样

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

你想TextBlock的文本设置为亮/暗或这个问题的答案? – AVK

+0

@AVKNaidu是例如 –

+0

您可以显示完整的代码,了解如何将文本设置为文本块? – AVK

回答

1

ApplicationThemeEnum为你检查什么主题是您的应用程序强加的。你可以检查下面。

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

更多信息Here

+0

这不起作用。 TextBlock始终显示为“Light” –

+0

如果您正在进行周年纪念更新,它会一直很亮,因为未实现黑暗。同时检查你的app.xaml的主题。您可能将其设置为Light而不是Default。 – AVK

+0

我的错是我将RequestedTheme设置为Light。如果我删除它,代码按预期工作。谢谢 –

0

你可以试试:

Application.Current.Resources["SystemAccentColor"] 
+0

只需指定[资源键](https://msdn.microsoft.com/en-us/windows/uwp/controls-and-patterns/xaml-theme-resources#light-and-dark-theme-colors)这表明,+1 –

+0

重音颜色和明/暗模式是两个完全独立的设置。重音颜色告诉你*无*有关系统是处于光照模式还是黑暗模式。 – BoltClock

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>