You can simplify the operation of saving, retrieving and selecting the Application theme, backdrop and TintColors. All operations are performed automatically.
Backdrops
Name |
---|
None |
Mica |
MicaAlt |
DesktopAcrylic |
AcrylicThin |
AcrylicBase |
Transparent |
Methods
Name |
---|
Window |
ConfigureBackdrop |
ConfigureTintColor |
ConfigureFallbackColor |
ConfigureElementTheme |
GetSystemBackdrop |
GetBackdropType |
GetElementTheme |
GetActualTheme |
SetBackdropType |
SetBackdropTintColor |
SetBackdropFallbackColor |
SetElementTheme |
SetElementThemeWithoutSave |
IsDarkTheme |
OnThemeComboBoxSelectionChanged |
SetThemeComboBoxDefaultItem |
OnBackdropComboBoxSelectionChanged |
SetBackdropComboBoxDefaultItem |
OnThemeRadioButtonChecked |
SetThemeRadioButtonDefaultItem |
OnBackdropRadioButtonChecked |
SetBackdropRadioButtonDefaultItem |
UpdateCaptionButtons |
Simple Usage
There is multipe ways you can use ThemeService:
In this method, all settings are applied automatically.
1 | var themeService = new ThemeService(window); |
or
1 | var themeService = new ThemeService(); |
If you want to change some settings like autosave, file location, use Initialize
method:
1 | themeService.Initialize(window, true, @"D:\app\config.json"); |
and if you want to save and restore theme manually you can set useAutoSave = false
1 | themeService.Initialize(window, false); |
Configs
there are some configure methods:
ConfigureBackdrop
If you use the ConfigureBackdrop
, the themeService will automatically save and restore the SystemBackdrop (if useAutoSave is true)
1 | themeService.Initialize(...).ConfigureBackdrop(BackdropType.Mica); |
ConfigureElementTheme
If you use the ConfigureElementTheme
, the themeService will automatically save and restore the ElementTheme (if useAutoSave is true)
1 | themeService.Initialize(...).ConfigureElementTheme(ElementTheme.Default); |
ConfigureTintColor
you can change system backdrop TintColor.
1 | themeService.Initialize(...).ConfigureTintColor(); |
then you can set your tint color:
1 | themeService.SetBackdropTintColor(Colors.Yellow); |
ConfigureFallbackColor
you can change system backdrop FallBackColor.
1 | themeService.Initialize(...).ConfigureFallbackColor(); |
then you can set your FallBackColor
1 | themeService.SetBackdropFallBackColor(); |
ConfigureTintColor and ConfigureFallbackColor only works if you use Acrylic or Mica Backdrop.
EnableRequestedTheme
This method enables the ability to use the Application.Current.RequestedTheme
.
1 | themeService.Initialize(...).EnableRequestedTheme(); |
now you can use Application.Current.RequestedTheme
in Runtime.
Changing ElementTheme in Runtime
you can change ElementTheme in Runtime like this:
1 | themeService.SetElementTheme(ElementTheme.Dark); |
Changing SystemBackdrop Type in Runtime
you can change SystemBackdrop Type in Runtime like this:
1 | themeService.SetBackdropType(BackdropType.Mica); |
Auto Change Theme/Backdrop and Auto Load Default Item
if you want to set defualt item for combobox or radiobuttons, and auto switch between themes or backdrops just add following line in your xaml
ComboBox
1 | <ComboBox dev:ThemeServiceAttach.ThemeService="{x:Bind local:App.Current.GetThemeService}"> |
RadioButtons
1 | <StackPanel dev:ThemeServiceAttach.ThemeService="{x:Bind local:App.Current.GetThemeService}"> |
Auto Change Theme/Backdrop and Auto Load Default Item (Old Methods)
ComboBox
if you want to handle everything by yourself, first of all remove dev:ThemeServiceAttach.ThemeService="{x:Bind local:App.Current.GetThemeService}"
and then add your events:
for changing and saving application theme / backdrop:
1 | private void cmbTheme_SelectionChanged(object sender, SelectionChangedEventArgs e) |
for selecting currect combobox item when page is loading, you can call SetThemeComboBoxDefaultItem
or SetBackdropComboBoxDefaultItem
method in Page Loaded
event:
1 | themeService.SetThemeComboBoxDefaultItem(cmb1); |
RadioButtons
if you want to handle everything by yourself, first of all remove dev:ThemeServiceAttach.ThemeService="{x:Bind local:App.Current.GetThemeService}"
and then add your events:
for changing and saving application theme / backdrop:
1 | private void OnThemeRadioButtonChecked(object sender, RoutedEventArgs e) |
for selecting currect radiobutton item when page is loading, you can call SetThemeRadioButtonDefaultItem
or SetBackdropRadioButtonDefaultItem
method in Page Loaded
event:
1 | themeService.SetThemeRadioButtonDefaultItem(ThemePanel1); |
Change Theme/Backdrop Manual
you can change Application Theme with RootTheme
, ActualTheme
and ChangeTheme
method:
1 | themeService.RootTheme = ElementTheme.Dark; |
you can change Application SystemBackdrop:
1 | themeService.SetBackdropType(BackdropType.Mica); |
MVVM Pattern
first register a IThemeService
service:
1 | services.AddSingleton<IThemeService, ThemeService>(); |
then in your App.xaml.cs
1 | var themeService = GetService<IThemeService>() as ThemeService; |
Demo
you can run demo and see this feature.