Ejemplo n.º 1
0
        private static void ApplyThemeInternal(this FrameworkElement control, Theme? theme, SolidColorBrush accentBrush, SolidColorBrush contrastBrush)
        {
            ValidationHelper.NotNull(control, () => control);

            // Resource dictionaries paths
            var lightBrushesUri = new Uri("/Elysium;component/Themes/LightBrushes.xaml", UriKind.Relative);
            var darkBrushesUri = new Uri("/Elysium;component/Themes/DarkBrushes.xaml", UriKind.Relative);

            // Resource dictionaries
            var lightBrushesDictionary = new ResourceDictionary { Source = lightBrushesUri };
            var darkBrushesDictionary = new ResourceDictionary { Source = darkBrushesUri };

            if (theme == Theme.Light)
            {
                // Add LightBrushes.xaml, if not included
                if (control.Resources.MergedDictionaries.All(dictionary => dictionary.Source != lightBrushesUri))
                {
                    control.Resources.MergedDictionaries.Add(lightBrushesDictionary);
                }

                // Remove DarkBrushes.xaml, if included
                var darkColorsDictionaries = control.Resources.MergedDictionaries.Where(dictionary => dictionary.Source == darkBrushesUri).ToList();
                foreach (var dictionary in darkColorsDictionaries)
                {
                    control.Resources.MergedDictionaries.Remove(dictionary);
                }
            }
            if (theme == Theme.Dark)
            {
                // Add DarkBrushes.xaml, if not included
                if (control.Resources.MergedDictionaries.All(dictionary => dictionary.Source != darkBrushesUri))
                {
                    control.Resources.MergedDictionaries.Add(darkBrushesDictionary);
                }

                // Remove LightBrushes.xaml, if included
                var lightColorsDictionaries = control.Resources.MergedDictionaries.Where(dictionary => dictionary.Source == lightBrushesUri).ToList();
                foreach (var dictionary in lightColorsDictionaries)
                {
                    control.Resources.MergedDictionaries.Remove(dictionary);
                }
            }

            // Bug in WPF 4: http://connect.microsoft.com/VisualStudio/feedback/details/555322/global-wpf-styles-are-not-shown-when-using-2-levels-of-references
            if (control.Resources.Keys.Count == 0)
            {
                control.Resources.Add(typeof(Window), new Style(typeof(Window)));
            }

            if (accentBrush != null)
            {
                var accentBrushFrozen = !accentBrush.IsFrozen && accentBrush.CanFreeze ? accentBrush.GetAsFrozen() : accentBrush;
                if (control.Resources.Contains("AccentBrush"))
                {
                    // Set AccentBrush value, if key exist
                    control.Resources["AccentBrush"] = accentBrushFrozen;
                }
                else
                {
                    // Add AccentBrush key and value, if key doesn't exist
                    control.Resources.Add("AccentBrush", accentBrushFrozen);
                }
            }

            if (contrastBrush != null)
            {
                var contrastBrushFrozen = !contrastBrush.IsFrozen && contrastBrush.CanFreeze ? contrastBrush.GetAsFrozen() : contrastBrush;
                if (control.Resources.Contains("ContrastBrush"))
                {
                    // Set ContrastBrush value, if key exist
                    control.Resources["ContrastBrush"] = contrastBrushFrozen;
                }
                else
                {
                    // Add ContrastBrush key and value, if key doesn't exist
                    control.Resources.Add("ContrastBrush", contrastBrushFrozen);
                }

                var semitransparentContrastBrush = contrastBrush.Clone();
                semitransparentContrastBrush.Opacity = 1d / 8d;
                var semitransparentContrastBrushFrozen = !semitransparentContrastBrush.IsFrozen && semitransparentContrastBrush.CanFreeze
                                                             ? semitransparentContrastBrush.GetAsFrozen()
                                                             : semitransparentContrastBrush;
                if (control.Resources.Contains("SemitransparentContrastBrush"))
                {
                    // Set SemitransparentContrastBrush value, if key exist
                    control.Resources["SemitransparentContrastBrush"] = semitransparentContrastBrushFrozen;
                }
                else
                {
                    // Add SemitransparentContrastBrush key and value, if key doesn't exist
                    control.Resources.Add("SemitransparentContrastBrush", semitransparentContrastBrushFrozen);
                }
            }

            // Add Generic.xaml, if not included
            var genericDictionaryUri = new Uri("/Elysium;component/Themes/Generic.xaml", UriKind.Relative);
            if (control.Resources.MergedDictionaries.All(dictionary => dictionary.Source != genericDictionaryUri))
            {
                control.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = genericDictionaryUri });
            }

            OnThemeChanged();
        }