示例#1
0
        public static ResourceDictionary CreateTheme(string baseColorScheme, Color accentBaseColor, string name = null,
                                                     bool changeImmediately = false)
        {
            name ??= $"RuntimeTheme_{baseColorScheme}_{accentBaseColor.ToString().Replace("#", string.Empty)}";

            var generatorParameters  = GetGeneratorParameters();
            var themeTemplateContent = GetThemeTemplateContent();

            var variant     = generatorParameters.BaseColorSchemes.First(x => x.Name == baseColorScheme);
            var colorScheme = new ColorScheme();

            colorScheme.Name = accentBaseColor.ToString().Replace("#", string.Empty);
            var values = colorScheme.Values;

            values.Add("MahApps.Colors.AccentBase", accentBaseColor.ToString());
            values.Add("MahApps.Colors.Accent",
                       Color.FromArgb(204, accentBaseColor.R, accentBaseColor.G, accentBaseColor.B).ToString());
            values.Add("MahApps.Colors.Accent2",
                       Color.FromArgb(153, accentBaseColor.R, accentBaseColor.G, accentBaseColor.B).ToString());
            values.Add("MahApps.Colors.Accent3",
                       Color.FromArgb(102, accentBaseColor.R, accentBaseColor.G, accentBaseColor.B).ToString());
            values.Add("MahApps.Colors.Accent4",
                       Color.FromArgb(51, accentBaseColor.R, accentBaseColor.G, accentBaseColor.B).ToString());

            values.Add("MahApps.Colors.Highlight", accentBaseColor.ToString());
            values.Add("MahApps.Colors.IdealForeground", IdealTextColor(accentBaseColor).ToString());

            var xamlContent = new ColorSchemeGenerator().GenerateColorSchemeFileContent(generatorParameters, variant,
                                                                                        colorScheme, themeTemplateContent, name, name);

            var resourceDictionary = (ResourceDictionary)XamlReader.Parse(xamlContent);

            var newTheme = new Theme(resourceDictionary);

            ThemeManager.AddTheme(newTheme.Resources);

            // Apply theme
            if (changeImmediately)
            {
                ThemeManager.ChangeTheme(Application.Current, newTheme);
            }

            return(resourceDictionary);
        }
        public static Tuple <string, ResourceDictionary> CreateTheme(string baseColorScheme, Color accentBaseColor, Color highlightColor, string name = null, bool changeImmediately = false)
        {
            name = name ?? GenerateThemeName(baseColorScheme, accentBaseColor, highlightColor);

            var generatorParameters  = GetGeneratorParameters();
            var themeTemplateContent = GetThemeTemplateContent();

            var variant     = generatorParameters.BaseColorSchemes.First(x => x.Name == baseColorScheme);
            var colorScheme = new XamlColorSchemeGenerator.ColorScheme
            {
                Name = accentBaseColor.ToString().Replace("#", string.Empty)
            };

            var values = colorScheme.Values;

            values.Add("Fluent.Ribbon.Colors.AccentBaseColor", accentBaseColor.ToString());
            values.Add("Fluent.Ribbon.Colors.AccentColor80", Color.FromArgb(204, accentBaseColor.R, accentBaseColor.G, accentBaseColor.B).ToString());
            values.Add("Fluent.Ribbon.Colors.AccentColor60", Color.FromArgb(153, accentBaseColor.R, accentBaseColor.G, accentBaseColor.B).ToString());
            values.Add("Fluent.Ribbon.Colors.AccentColor40", Color.FromArgb(102, accentBaseColor.R, accentBaseColor.G, accentBaseColor.B).ToString());
            values.Add("Fluent.Ribbon.Colors.AccentColor20", Color.FromArgb(51, accentBaseColor.R, accentBaseColor.G, accentBaseColor.B).ToString());

            values.Add("Fluent.Ribbon.Colors.HighlightColor", highlightColor.ToString());
            values.Add("Fluent.Ribbon.Colors.IdealForegroundColor", IdealTextColor(accentBaseColor).ToString());

            var xamlContent = new ColorSchemeGenerator().GenerateColorSchemeFileContent(generatorParameters, variant, colorScheme, themeTemplateContent, name, name);

            var resourceDictionary = (ResourceDictionary)XamlReader.Parse(xamlContent);

            var newTheme = new Theme(resourceDictionary);

            ThemeManager.AddTheme(newTheme.Resources);

            // Apply theme
            if (changeImmediately)
            {
                ThemeManager.ChangeTheme(Application.Current, newTheme);
            }

            return(new Tuple <string, ResourceDictionary>(xamlContent, resourceDictionary));
        }
        public string GenerateColorSchemeFileContent(GeneratorParameters parameters, BaseColorScheme baseColorScheme, ColorScheme colorScheme, string templateContent, string themeName, string themeDisplayName)
        {
            templateContent = templateContent.Replace("{{ThemeName}}", themeName);
            templateContent = templateContent.Replace("{{ThemeDisplayName}}", themeDisplayName);
            templateContent = templateContent.Replace("{{BaseColorScheme}}", baseColorScheme.Name);
            templateContent = templateContent.Replace("{{ColorScheme}}", colorScheme.Name);

            foreach (var value in colorScheme.Values)
            {
                templateContent = templateContent.Replace($"{{{{{value.Key}}}}}", value.Value);
            }

            foreach (var value in baseColorScheme.Values)
            {
                templateContent = templateContent.Replace($"{{{{{value.Key}}}}}", value.Value);
            }

            foreach (var value in parameters.DefaultValues)
            {
                templateContent = templateContent.Replace($"{{{{{value.Key}}}}}", value.Value);
            }

            return(templateContent);
        }
        public void GenerateColorSchemeFile(GeneratorParameters parameters, string templateDirectory, string templateContent, BaseColorScheme baseColorScheme, ColorScheme colorScheme)
        {
            var themeName         = $"{baseColorScheme.Name}.{colorScheme.Name}";
            var themeDisplayName  = $"{colorScheme.Name} ({baseColorScheme.Name})";
            var themeFilename     = $"{themeName}.xaml";
            var themeTempFileName = $"{themeFilename}_{Guid.NewGuid()}.xaml";

            var themeFile     = Path.Combine(templateDirectory, themeFilename);
            var themeTempFile = Path.Combine(Path.GetTempPath(), themeTempFileName);

            try
            {
                var fileContent = this.GenerateColorSchemeFileContent(parameters, baseColorScheme, colorScheme, templateContent, themeName, themeDisplayName);

                File.WriteAllText(themeTempFile, fileContent, Encoding.UTF8);

                Trace.WriteLine($"Comparing temp file \"{themeTempFile}\" to \"{themeFile}\"");

                if (File.Exists(themeFile) == false ||
                    File.ReadAllText(themeFile) != File.ReadAllText(themeTempFile))
                {
                    File.Copy(themeTempFile, themeFile, true);

                    Trace.WriteLine($"Resource Dictionary saved to \"{themeFile}\".");
                }
                else
                {
                    Trace.WriteLine("New Resource Dictionary did not differ from existing file. No new file written.");
                }
            }
            finally
            {
                if (File.Exists(themeTempFile))
                {
                    File.Delete(themeTempFile);
                }
            }
        }