如何从特定颜色创建 MahApps.Metro 重音资源?

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

我在 MahApps site(页面底部)上注意到了这一点:"It is also possible to create an accent resource dictionary dynamically by using a specific color.",但我无处可寻。

真的有内置方法(或其他方法)吗?
我只找到了 ThemeManager.AddAccent(string name, Uri resourceAddress),并且可以使用 [=12 创建新的口音(当前) =],因此无论如何都需要名称和资源 uri...
有什么想法吗?

下面是创建动态资源字典并将其添加到 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);
        }
    }
}

用法:

ThemeManagerHelper.CreateAppStyleBy(Colors.Indigo, true);

这取自我在 GitHub

上托管的代码示例 (MahAppsMetroThemesSample)

希望对您有所帮助!

ThemeManager 已更改,现在原生支持从颜色创建自定义主题。

这是来自 MahApps documentation 的示例:

// Add Dark and Light versions of DarkRed
ThemeManager.Current.AddTheme(new Theme("CustomDarkRed", "CustomDarkRed", "Dark", "Red", Colors.DarkRed, Brushes.DarkRed, true, false));
ThemeManager.Current.AddTheme(new Theme("CustomLightRed", "CustomLightRed", "Light", "Red", Colors.DarkRed, Brushes.DarkRed, true, false));

//Set current theme to CustomDarkRed
ThemeManager.Current.ChangeTheme(this, "CustomDarkRed");