Change colors for theme aware applications for Windows Phone 7

大兔子大兔子 提交于 2019-12-24 05:39:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!