Notes
This Class
is based on the ThemeSelectorService
from the Windows Template Studio, but with some
changes to make it work with the ThemeTitleBarService
from the UWP Toolkit. You can delete the ThemeSelectorService
from the
Windows Template Studio
if you use the ThemeTitleBarService
from the UWP Toolkit in your project.
Or you can also use ThemeTitleBarService
inside your ThemeSelectorService
from the Windows Template Studio.
Example:
- Put
private static ThemeTitleBarService themeTitleBar;
on top of theSettingsKey const
. - Add
themeTitleBar = new ThemeTitleBarService();
andthemeTitleBar.SetTheme();
in theInitializeAsync
method.
public static async Task InitializeAsync()
{
Theme = await LoadThemeFromSettingsAsync();
themeTitleBar = new ThemeTitleBarService();
//Theme Default
themeTitleBar.SetTheme();
}
- Add
themeTitleBar.SetTheme();
in theSetRequestedThemeAsync
method.
public static async Task SetRequestedThemeAsync()
{
foreach (var view in CoreApplication.Views)
{
await view.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
if (Window.Current.Content is FrameworkElement frameworkElement)
{
frameworkElement.RequestedTheme = Theme;
// Change Theme manually
themeTitleBar.SetTheme();
}
});
}
}
- Add
ThemeTitleBarService.CurrentWindow = Window.Current;
in theApp
Method.
// App.xaml.cs
public App()
{
InitializeComponent();
// <...>
// Deferred execution until used. Check https://docs.microsoft.com/dotnet/api/system.lazy-1 for further info on Lazy<T> class.
_activationService = new Lazy<ActivationService>(CreateActivationService);
ThemeTitleBarService.CurrentWindow = Window.Current;
}
Initialize ThemeSelectorService, loading saved theme from settings and Initialize ThemeTitleBarService
.
Sets the application theme
, updates the user interface and saves the theme in the application settings.
Configures the requested theme
in all application views, updating the user interface and the theme title bar
.