Switching between light theme and dark theme

21 Jun 20201 minute to read

To switch to light theme from dark theme, use the following code snippet.

  • c#
  • 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.