问题
I'm wanting to change the color of a rectangle depending on what theme the user has chosen on their phone.
EG. When the user has their device's theme color set to light a rectangle should be blue and when the theme is set to dark it should be a grey.
Any ideas?
Thanks
回答1:
I wrote a custom resource dictionary implementation which selects another dictionary at runtime without a performance penalty and works in the Visual Studio designer. You would use it like this:
<Application.Resources>
<custom:ThemeResourceDictionary>
<custom:ThemeResourceDictionary.LightResources>
<ResourceDictionary Source="/ThemeManagement;component/Resources/Light.xaml" />
</custom:ThemeResourceDictionary.LightResources>
<custom:ThemeResourceDictionary.DarkResources>
<ResourceDictionary Source="/ThemeManagement;component/Resources/Dark.xaml" />
</custom:ThemeResourceDictionary.DarkResources>
</custom:ThemeResourceDictionary>
</Application.Resources>
Where Light.xaml
and Dark.xaml
would contain resources with the same names.
You can get the code and read more about it on my blog.
回答2:
This kind of thing would be able to determine what the theme is set to (dark or light). You might want to build it into a property that you can bind to for your brush.
Visibility v = (Visibility)Resources["PhoneLightThemeVisibility"];
if (v == System.Windows.Visibility.Visible)
{
// set your brush to blue
}
else
{
// set your brush to grey
}
You can also get the user's selected accent colour with the PhoneAccentBrush
if you need to take that into account as well.
来源:https://stackoverflow.com/questions/6651312/change-colors-for-theme-aware-applications-for-windows-phone-7