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

我在MahApps网站 (页面底部)注意到了这一点:“也可以通过使用特定颜色动态创建重音资源字典。”但我发现更多地方无处可寻。

是否真的有内置方法(或其他任何东西)?
我只找到了ThemeManager.AddAccent(string name, Uri resourceAddress) ,并使用new Accent(string name, Uri resourceAddress)创建了一个新的重音(当前new Accent(string name, Uri resourceAddress) ,因此名称和资源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)

希望这可以帮助!