Skip to content

Change Theme or Accent color (Version before 5.0.0)

Simnico99 edited this page Mar 3, 2023 · 3 revisions

Click here for versions 5.0.0 and newer

Contents

Table of Contents
  1. AccentColorService
  2. ThemeService
  3. Depedency Injection

AccentColorService

Usage

This service uses the singleton pattern so only one Class is initialized by software. In order to use it you have to use

AccentColorService.Current

instead of

new AccentColorService()

Methods

UpdateAccentsFromWindows()

This method will set your software to use the user current defined Windows accent color as the software default accent color. (Enabled by default)

Usage Exemple:
AccentColorService.Current.UpdateAccentsFromWindows();
Results:

image image

UpdateAccents(Color)

This method will set your software to use a custom Color as your Accent Color and will ignore the Windows Accent color.

Usage Exemple:
AccentColorService.Current.UpdateAccents(System.Windows.Media.Color.FromRgb(255, 0, 0));
Results:

image image

ThemeService

Usage

This service uses the singleton pattern so only one Class is initialized by software. In order to use it you have to use

ThemeService.Current

instead of

new ThemeService()

Methods

ChangeTheme(WindowsTheme)

This method will set your software current theme. (Light or Dark)

Usage Exemple:
ThemeService.Current.ChangeTheme(WindowsTheme.Light);
Results:

image

EnableBackdrop(Window, BackdropType, int(CaptionHeight))

This method will enable Mica on the specified window it is recommended to use the extension instead.

Usage Exemple:
ThemeService.Current.EnableBackdrop(this, BackdropType.Mica, 20);
Results:

image

GetWindowsTheme()

This method allows you to get the current Windows theme.

Usage Exemple:
var currentTheme = ThemeService.GetWindowsTheme();
Console.WriteLine(currentTheme.ToString());
Results:

Console output: Dark (As I am using Dark Theme on Windows)

WindowsThemeToResourceTheme(WindowsTheme)

This method allows you to get the Ressource Uri for the specified theme.

Usage Exemple:
var themeRessource = ThemeService.WindowsThemeToResourceTheme(WindowsTheme.Dark);
Console.WriteLine(themeRessource.ToString());
Results:

Console output: pack://application:,,,/MicaWPF;component/Styles/Themes/MicaDark.xaml

Properties

IsThemeAware { get; }

Will tell if the service is listening for theme change.

Usage Exemple:
var currentThemeAwareness = ThemeService.Current.IsThemeAware;
Console.WriteLine(currentThemeAwareness); //True by default

Dependency Injection

Both services are accessible via DependencyInjection see dependency injection implementation.