/// <summary>
 /// Creates a new instance of this class.
 /// </summary>
 public OnThemeChangedEventArgs(AppTheme appTheme, Accent accent)
 {
     this.AppTheme = appTheme;
     this.Accent   = accent;
 }
 private static void OnThemeChanged(Accent newAccent, AppTheme newTheme)
 {
     IsThemeChanged?.Invoke(Application.Current, new OnThemeChangedEventArgs(newTheme, newAccent));
 }
        public static void ChangeAppStyle(ResourceDictionary resources, Accent newAccent, AppTheme newTheme)
        {
            if (resources == null)
            {
                throw new ArgumentNullException(nameof(resources));
            }

            if (newAccent == null)
            {
                throw new ArgumentNullException(nameof(newAccent));
            }

            if (newTheme == null)
            {
                throw new ArgumentNullException(nameof(newTheme));
            }

            ApplyResourceDictionary(newAccent.Resources, resources);
            ApplyResourceDictionary(newTheme.Resources, resources);
        }
        internal static bool DetectThemeFromAppResources(out AppTheme detectedTheme)
        {
            detectedTheme = null;

            return(DetectThemeFromResources(ref detectedTheme, Application.Current.Resources));
        }
        private static void ChangeAppStyle(ResourceDictionary resources, Tuple <AppTheme, Accent> oldThemeInfo, Accent newAccent, AppTheme newTheme)
        {
            var themeChanged = false;

            if (oldThemeInfo != null)
            {
                var oldAccent = oldThemeInfo.Item2;
                if (oldAccent != null && oldAccent.Name != newAccent.Name)
                {
                    resources.MergedDictionaries.Add(newAccent.Resources);

                    var key = oldAccent.Resources.Source.ToString().ToLower();
                    var oldAccentResource = resources.MergedDictionaries.Where(x => x.Source != null).FirstOrDefault(d => d.Source.ToString().ToLower() == key);
                    if (oldAccentResource != null)
                    {
                        resources.MergedDictionaries.Remove(oldAccentResource);
                    }

                    themeChanged = true;
                }

                var oldTheme = oldThemeInfo.Item1;
                if (oldTheme != null && oldTheme != newTheme)
                {
                    resources.MergedDictionaries.Add(newTheme.Resources);

                    var key = oldTheme.Resources.Source.ToString().ToLower();
                    var oldThemeResource = resources.MergedDictionaries.Where(x => x.Source != null).FirstOrDefault(d => d.Source.ToString().ToLower() == key);
                    if (oldThemeResource != null)
                    {
                        resources.MergedDictionaries.Remove(oldThemeResource);
                    }

                    themeChanged = true;
                }
            }
            else
            {
                ChangeAppStyle(resources, newAccent, newTheme);

                themeChanged = true;
            }

            if (themeChanged)
            {
                OnThemeChanged(newAccent, newTheme);
            }
        }