Example #1
0
        private static void ApplyThemeResourceDictionary <T>(ResourceDictionary resources, Theme theme, T themeResourceDictionary) where T : ThemeResourceDictionary, new()
        {
            if (theme == null)
            {
                return;
            }

            if (themeResourceDictionary?.Theme != theme)
            {
                themeResourceDictionary = new T {
                    Source = theme.Source, Theme = theme
                }
            }
            ;

            resources.MergedDictionaries.Add(themeResourceDictionary);
        }
    }
Example #2
0
        private static void ApplyTheme(object sender, ResourceDictionary resources, Theme baseTheme, Theme colorTheme)
        {
            Debug.Assert(resources != null);

            if (WindowsHelper.IsInDesignMode)
            {
                // Don't do anything at design-time. (Could mess up Expression Blend.)
                return;
            }

            // Clear previous theme.
            var baseThemeResourceDictionary  = ClearThemeResourceDictionary <BaseThemeResourceDictionary>(resources);
            var colorThemeResourceDictionary = ClearThemeResourceDictionary <ColorThemeResourceDictionary>(resources);

            // Apply new Theme.
            ApplyThemeResourceDictionary(resources, baseTheme, baseThemeResourceDictionary);
            ApplyThemeResourceDictionary(resources, colorTheme, colorThemeResourceDictionary);

            // Raise ThemeChanged event.
            var oldBaseTheme  = baseThemeResourceDictionary?.Theme;
            var oldColorTheme = colorThemeResourceDictionary?.Theme;
            var eventArgs     = new ThemeChangedEventArgs(oldBaseTheme, oldColorTheme, baseTheme, colorTheme);

            ThemeChangedEvent.Invoke(sender, eventArgs);
        }