[英]How to create MahApps.Metro accent resource from specific color?
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.我在MahApps 站点(页面底部)上注意到了这一点:“也可以通过使用特定颜色动态创建重音资源字典。”,但我找不到更多。
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...我只找到了
ThemeManager.AddAccent(string name, Uri resourceAddress)
,并且可以使用new Accent(string name, Uri resourceAddress)
创建一个新的口音(当前new Accent(string name, Uri resourceAddress)
,所以总是需要一个名称和一个资源 uri ......
Any ideas?有任何想法吗?
Here is a simple sample to create a dynamic resource dictionary and add it to the ThemeManager
:这是创建动态资源字典并将其添加到
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这是从我托管在GitHub 上的代码示例 (MahAppsMetroThemesSample) 中获取的
Hope this helps!希望这可以帮助!
ThemeManager has since been changed and now natively supports creating a custom theme from a color. ThemeManager 已经更改,现在本机支持从颜色创建自定义主题。
Here's an example from MahApps documentation :这是 MahApps文档中的一个示例:
// 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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.