Ejemplo n.º 1
0
        /// <summary>
        /// 可以在配置文件进行设置
        /// </summary>
        public void SetDefaultTheme(ThemeType type)
        {
            _currentTheme = type;

            _themeProvider = _themeProvider ?? new ThemeProvider();

            var themeColor = _themeProvider.ThemeColors[type.ToString().ToLower()];

            var parentDictionary = Application.Current.Resources;

            var primarySource           = $"pack://application:,,,/{Assembly.GetExecutingAssembly().GetName().Name};component/ColorBox/{Enum.GetName(typeof(ThemeType),type)}_primary.xaml";
            var primarySourceDictionary = new ResourceDictionary()
            {
                Source = new Uri(primarySource)
            };

            Application.Current.Resources.MergedDictionaries.Add(primarySourceDictionary);

            var accentSource           = $"pack://application:,,,/{Assembly.GetExecutingAssembly().GetName().Name};component/ColorBox/{Enum.GetName(typeof(ThemeType), type)}_accent.xaml";
            var accentSourceDictionary = new ResourceDictionary()
            {
                Source = new Uri(accentSource)
            };

            Application.Current.Resources.MergedDictionaries.Add(accentSourceDictionary);
        }
Ejemplo n.º 2
0
        public void SetCurrentTheme(ThemeType type)
        {
            if (type != _currentTheme)
            {
                _currentTheme = type;

                _themeProvider = _themeProvider ?? new ThemeProvider();

                var themeColor = _themeProvider.ThemeColors[type.ToString().ToLower()];

                var parentDictionary = Application.Current.Resources;

                foreach (var item in Enum.GetNames(typeof(AccentBrushes)))
                {
                    var brush = parentDictionary[item] as SolidColorBrush;

                    if (item.StartsWith("AccentLight"))
                    {
                        var animation = new ColorAnimation
                        {
                            From     = brush.Color,
                            To       = SetBrushColor(0, item),
                            Duration = new Duration(TimeSpan.FromMilliseconds(300))
                        };
                        brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
                        continue;
                    }

                    if (item.StartsWith("AccentMid"))
                    {
                        var animation = new ColorAnimation
                        {
                            From     = brush.Color,
                            To       = SetBrushColor(2, item),
                            Duration = new Duration(TimeSpan.FromMilliseconds(300))
                        };
                        brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
                        continue;
                    }

                    if (item.StartsWith("AccentDark"))
                    {
                        var animation = new ColorAnimation
                        {
                            From     = brush.Color,
                            To       = SetBrushColor(3, item),
                            Duration = new Duration(TimeSpan.FromMilliseconds(300))
                        };
                        brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
                        continue;
                    }

                    Color SetBrushColor(int index, string name)
                    {
                        if (item.Contains("Foreground"))
                        {
                            return(themeColor.AccentColors.ToArray()[index].ForeColor);
                        }
                        else
                        {
                            return(themeColor.AccentColors.ToArray()[index].BackColor);
                        }
                    }
                }

                foreach (var item in Enum.GetNames(typeof(PrimaryBrushes)))
                {
                    var brush = parentDictionary[item] as SolidColorBrush;

                    if (item.StartsWith("PrimaryLight"))
                    {
                        var animation = new ColorAnimation
                        {
                            From     = brush.Color,
                            To       = SetBrushColor(0, item),
                            Duration = new Duration(TimeSpan.FromMilliseconds(300))
                        };
                        brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
                        continue;
                    }

                    if (item.StartsWith("PrimaryMid"))
                    {
                        var animation = new ColorAnimation
                        {
                            From     = brush.Color,
                            To       = SetBrushColor(4, item),
                            Duration = new Duration(TimeSpan.FromMilliseconds(300))
                        };
                        brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
                        continue;
                    }

                    if (item.StartsWith("PrimaryDark"))
                    {
                        var animation = new ColorAnimation
                        {
                            From     = brush.Color,
                            To       = SetBrushColor(8, item),
                            Duration = new Duration(TimeSpan.FromMilliseconds(300))
                        };
                        brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
                        continue;
                    }

                    Color SetBrushColor(int index, string name)
                    {
                        if (name.Contains("Foreground"))
                        {
                            return(themeColor.PrimaryColors.ToArray()[index].ForeColor);
                        }
                        else
                        {
                            return(themeColor.PrimaryColors.ToArray()[index].BackColor);
                        }
                    }
                }

                var existingResourceDictionary = Application.Current.Resources.MergedDictionaries
                                                 .Where(rd => rd.Source != null)
                                                 .Where(rd => Regex.Match(rd.Source.OriginalString, @"(\/Resource;component\/ColorBox\/([a-z]+)_(accent)|(primary))").Success).ToList();

                if (existingResourceDictionary.Any())
                {
                    foreach (var item in existingResourceDictionary)
                    {
                        Application.Current.Resources.MergedDictionaries.Remove(item);
                    }
                }

                var primarySource           = $"pack://application:,,,/{Assembly.GetExecutingAssembly().GetName().Name};component/ColorBox/{Enum.GetName(typeof(ThemeType), type)}_primary.xaml";
                var primarySourceDictionary = new ResourceDictionary()
                {
                    Source = new Uri(primarySource)
                };

                Application.Current.Resources.MergedDictionaries.Add(primarySourceDictionary);

                var accentSource           = $"pack://application:,,,/{Assembly.GetExecutingAssembly().GetName().Name};component/ColorBox/{Enum.GetName(typeof(ThemeType), type)}_accent.xaml";
                var accentSourceDictionary = new ResourceDictionary()
                {
                    Source = new Uri(primarySource)
                };

                Application.Current.Resources.MergedDictionaries.Add(accentSourceDictionary);
            }
        }