1

我的列表框项目上有网格,我想根据浅色或深色主题选择它们的背景颜色。

例如,如果我在浅色主题中,我的网格的背景颜色将为红色,如果我在黑暗中,颜色将为蓝色。我怎样才能在 Xaml 中做到这一点?

..................
   <Grid Background="#3F0E0D0D"> //in this case is fix for both themes...
.................
    </grid>
...............
4

2 回答 2

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如果你使用event 或Application_Launchingand 会更好Application_Activated

于 2013-04-17T06:49:30.103 回答
0

您不能直接在 XAML 中执行此操作,因为这是硬编码的。而是给你的 Grid 一个名字:

<Grid x:Name="MainGrid" Background="#3F0E0D0D"/>
...

然后在您的代码中检查主题(如@radoslaf 所示)并调用:

if(isDark)
   MainGrid.Background=.... ; // whatever color is needed
else
   MainGrid.Background=.... ; // whatever color is needed
于 2013-04-17T11:52:15.510 回答