0
\$\begingroup\$

I have the following command bonded to my button, It's supposed detect the current used theme and change it (Dark or Light).

  var existingResourceDictionary = Application.Current.Resources.MergedDictionaries
                .Where(rd => rd.Source != null)
                .SingleOrDefault(rd => Regex.Match(rd.Source.OriginalString, @"(\/MaterialDesignThemes.Wpf;component\/Themes\/MaterialDesignTheme\.)((Light)|(Dark))").Success);

            if (existingResourceDictionary == null)
                throw new ApplicationException("Unable to find Light/Dark base theme in Application resources.");


            if (existingResourceDictionary.Source.ToString().Contains("Light"))
            {
                Settings.Default.Theme = true;
                Settings.Default.Save();
            }
            else
            {
                Settings.Default.Theme = false;
                Settings.Default.Save();
            }

            var source =
                $"pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.{(Settings.Default.Theme ? "Dark" : "Light")}.xaml";
            var newResourceDictionary = new ResourceDictionary() { Source = new Uri(source) };

            Application.Current.Resources.MergedDictionaries.Remove(existingResourceDictionary);
            Application.Current.Resources.MergedDictionaries.Add(newResourceDictionary);

and in the ViewModel (when I run the application) It should detect the last changes made with the button above and apply the theme. I'm basically getting the value from the Settings that I changed above:

   var existingResourceDictionary = Application.Current.Resources.MergedDictionaries
          .Where(rd => rd.Source != null)
          .SingleOrDefault(rd => Regex.Match(rd.Source.OriginalString, @"(\/MaterialDesignThemes.Wpf;component\/Themes\/MaterialDesignTheme\.)((Light)|(Dark))").Success);

            if (existingResourceDictionary == null)
                throw new ApplicationException("Unable to find Light/Dark base theme in Application resources.");


            var source =
                $"pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.{(Settings.Default.Theme ? "Dark" : "Light")}.xaml";
            var newResourceDictionary = new ResourceDictionary() { Source = new Uri(source) };

            Application.Current.Resources.MergedDictionaries.Remove(existingResourceDictionary);
            Application.Current.Resources.MergedDictionaries.Add(newResourceDictionary);

The code is working fine but I feel like I could make it less repetitive and shorter?

\$\endgroup\$
1
\$\begingroup\$

You can substitute

if (existingResourceDictionary.Source.ToString().Contains("Light"))
{
    Settings.Default.Theme = true;
    Settings.Default.Save();
}
else
{
    Settings.Default.Theme = false;
    Settings.Default.Save();
}

by

Settings.Default.Theme = existingResourceDictionary.Source.ToString().Contains("Light");
Settings.Default.Save();

to shorten it a bit.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.