Switching between light theme and dark theme
9 Jan 20251 minute to read
To switch to light theme from dark theme, use the following code snippet.
void UpdateTheme(object sender, System.EventArgs e)
{
ICollection<ResourceDictionary> mergedDictionaries = Application.Current.Resources.MergedDictionaries;
var darkTheme = mergedDictionaries.OfType<DarkTheme>().FirstOrDefault();
if (darkTheme != null)
{
mergedDictionaries.Remove(darkTheme);
}
mergedDictionaries.Add(new LightTheme());
}
Similarly, to switch to dark theme from light theme, remove the light theme resource, and add the dark theme resource dictionary.
The complete Theme switch sample is available in this link.