【问题标题】:How to choose a background color according to windows phone theme in xaml如何根据 xaml 中的 windows phone 主题选择背景颜色
【发布时间】:2013-04-17 06:05:46
【问题描述】:
我的列表框项目上有网格,我想根据浅色或深色主题选择它们的背景颜色。
例如,如果我使用浅色主题,则网格的背景颜色将为红色,如果我使用深色,则颜色将为蓝色。我怎样才能在 Xaml 中做到这一点?
..................
<Grid Background="#3F0E0D0D"> //in this case is fix for both themes...
.................
</grid>
...............
【问题讨论】:
标签:
silverlight
xaml
colors
windows-phone-8
themes
【解决方案1】:
您可以通过非常简单的方式在手机上获取主题。
例如,您可以执行以下操作:
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
if (darkBackgroundVisibility == Visibility.Visible)
{
//theme is dark
//change grind background
}
else
{
//theme is light
}
}
如果您使用OnNavigatedTo 事件或Application_Launchingand 会更好
Application_Activated
【解决方案2】:
您不能直接在 XAML 中执行此操作,因为这是硬编码的。而是给你的 Grid 一个名字:
<Grid x:Name="MainGrid" Background="#3F0E0D0D"/>
...
然后在您的代码中检查主题(如@radoslaf 所示)并调用:
if(isDark)
MainGrid.Background=.... ; // whatever color is needed
else
MainGrid.Background=.... ; // whatever color is needed