Example #1
0
        private static void Activate(ThemeType type, string name)
        {
            var app = Application.Current;

            var mergedDictionaries = app.Resources.MergedDictionaries;

            var currentTheme = mergedDictionaries
                               .OfType <ThemeResourceDictionary>()
                               .FirstOrDefault(d => d.Type == type);

            var newTheme = ThemeResourceDictionary.Load(type, name);

            if (currentTheme == newTheme)
            {
                return;
            }

            if (newTheme != null)
            {
                mergedDictionaries.Add(newTheme);
            }

            if (currentTheme != null)
            {
                mergedDictionaries.Remove(currentTheme);
            }
        }
        public static ThemeResourceDictionary Load(ThemeType type, string name)
        {
            var uri = new Uri(
                string.Format("ui/Resources/Themes/{0}/{1}.xaml", type, name),
                UriKind.Relative
                );

            List <ThemeResourceDictionary> themes;
            ThemeResourceDictionary        dictionary = null;

            if (loadedDictionaries.TryGetValue(type, out themes))
            {
                dictionary = themes.FirstOrDefault(d => d.Source == uri);
            }

            return(dictionary
                   ?? new ThemeResourceDictionary {
                Source = uri, Type = type
            });
        }