Switching between light theme and dark theme

14 Oct 20211 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.

    The complete Theme switch sample is available in this link.