Change colors for theme aware applications for Win

2019-07-19 01:47发布

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

2条回答
Summer. ? 凉城
2楼-- · 2019-07-19 02:36

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.

查看更多
看我几分像从前
3楼-- · 2019-07-19 02:40

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.

查看更多
登录 后发表回答