How to create MahApps.Metro accent resource from specific color?

烂漫一生 提交于 2019-12-10 11:57:57

问题


I've noticed this on the MahApps site (bottom of the page): "It is also possible to create an accent resource dictionary dynamically by using a specific color.", but I found more nowhere.

Is there really a built-in method (or anything else) for this?
I've only found ThemeManager.AddAccent(string name, Uri resourceAddress), and creating a new accent (currently) possible with new Accent(string name, Uri resourceAddress), so a name and a resource uri is everyhow needed...
Any ideas?


回答1:


Here is a simple sample to create a dynamic resource dictionary and add it to the ThemeManager:

public static class ThemeManagerHelper
{
    public static void CreateAppStyleBy(Color color, bool changeImmediately = false)
    {
        // create a runtime accent resource dictionary

        var resourceDictionary = new ResourceDictionary();

        resourceDictionary.Add("HighlightColor", color);
        resourceDictionary.Add("AccentColor", Color.FromArgb((byte)(204), color.R, color.G, color.B));
        resourceDictionary.Add("AccentColor2", Color.FromArgb((byte)(153), color.R, color.G, color.B));
        resourceDictionary.Add("AccentColor3", Color.FromArgb((byte)(102), color.R, color.G, color.B));
        resourceDictionary.Add("AccentColor4", Color.FromArgb((byte)(51), color.R, color.G, color.B));

        resourceDictionary.Add("HighlightBrush", new SolidColorBrush((Color)resourceDictionary["HighlightColor"]));
        resourceDictionary.Add("AccentColorBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
        resourceDictionary.Add("AccentColorBrush2", new SolidColorBrush((Color)resourceDictionary["AccentColor2"]));
        resourceDictionary.Add("AccentColorBrush3", new SolidColorBrush((Color)resourceDictionary["AccentColor3"]));
        resourceDictionary.Add("AccentColorBrush4", new SolidColorBrush((Color)resourceDictionary["AccentColor4"]));
        resourceDictionary.Add("WindowTitleColorBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));

        resourceDictionary.Add("ProgressBrush", new LinearGradientBrush(
            new GradientStopCollection(new[]
            {
                new GradientStop((Color)resourceDictionary["HighlightColor"], 0),
                new GradientStop((Color)resourceDictionary["AccentColor3"], 1)
            }),
            new Point(0.001, 0.5), new Point(1.002, 0.5)));

        resourceDictionary.Add("CheckmarkFill", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
        resourceDictionary.Add("RightArrowFill", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));

        resourceDictionary.Add("IdealForegroundColor", Colors.White);
        resourceDictionary.Add("IdealForegroundColorBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));
        resourceDictionary.Add("AccentSelectedColorBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));

        // DataGrid brushes since latest alpha after 1.1.2
        resourceDictionary.Add("MetroDataGrid.HighlightBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
        resourceDictionary.Add("MetroDataGrid.HighlightTextBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));
        resourceDictionary.Add("MetroDataGrid.MouseOverHighlightBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor3"]));
        resourceDictionary.Add("MetroDataGrid.FocusBorderBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
        resourceDictionary.Add("MetroDataGrid.InactiveSelectionHighlightBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor2"]));
        resourceDictionary.Add("MetroDataGrid.InactiveSelectionHighlightTextBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));

        // applying theme to MahApps

        var resDictName = string.Format("ApplicationAccent_{0}.xaml", color.ToString().Replace("#", string.Empty));
        var fileName = Path.Combine(Path.GetTempPath(), resDictName);
        using (var writer = System.Xml.XmlWriter.Create(fileName, new System.Xml.XmlWriterSettings { Indent = true }))
        {
            System.Windows.Markup.XamlWriter.Save(resourceDictionary, writer);
            writer.Close();
        }

        resourceDictionary = new ResourceDictionary() { Source = new Uri(fileName, UriKind.Absolute) };

        var newAccent = new Accent { Name = resDictName, Resources = resourceDictionary };
        ThemeManager.AddAccent(newAccent.Name, newAccent.Resources.Source);

        if (changeImmediately)
        {
            var application = Application.Current;
            var applicationTheme = ThemeManager.AppThemes.First(x => string.Equals(x.Name, "BaseLight"));
            ThemeManager.ChangeAppStyle(application, newAccent, applicationTheme);
        }
    }
}

Usage:

ThemeManagerHelper.CreateAppStyleBy(Colors.Indigo, true);

This is taken from my code samples (MahAppsMetroThemesSample) hosted at GitHub

Hope this helps!



来源:https://stackoverflow.com/questions/32701013/how-to-create-mahapps-metro-accent-resource-from-specific-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!