packages/MaterialDesignThemes.2.5.1/src/net45/PaletteHelper.cs
(开头部分) 13KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using MaterialDesignColors;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace MaterialDesignThemes.Wpf
{
public class PaletteHelper
{
public virtual void SetLightDark(bool isDark)
{
var existingResourceDictionary = Application.Current.Resources.MergedDictionaries
.Where(rd => rd.Source != null)
.SingleOrDefault(rd => Regex.Match(rd.Source.OriginalString, @"(\/MaterialDesignThemes.Wpf;component\/Themes\/MaterialDesignTheme\.)((Light)|(Dark))").Success);
if (existingResourceDictionary == null)
throw new ApplicationException("Unable to find Light/Dark base theme in Application resources.");
var source =
$"pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.{(isDark ? "Dark" : "Light")}.xaml";
var newResourceDictionary = new ResourceDictionary() { Source = new Uri(source) };
Application.Current.Resources.MergedDictionaries.Remove(existingResourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(newResourceDictionary);
var existingMahAppsResourceDictionary = Application.Current.Resources.MergedDictionaries
.Where(rd => rd.Source != null)
.SingleOrDefault(rd => Regex.Match(rd.Source.OriginalString, @"(\/MahApps.Metro;component\/Styles\/Accents\/)((BaseLight)|(BaseDark))").Success);
if (existingMahAppsResourceDictionary == null) return;
source =
$"pack://application:,,,/MahApps.Metro;component/Styles/Accents/{(isDark ? "BaseDark" : "BaseLight")}.xaml";
var newMahAppsResourceDictionary = new ResourceDictionary { Source = new Uri(source) };
Application.Current.Resources.MergedDictionaries.Remove(existingMahAppsResourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(newMahAppsResourceDictionary);
}
/// <summary>
/// Replaces the entire palette
/// </summary>
public virtual void ReplacePalette(Palette palette)
{
if (palette == null) throw new ArgumentNullException(nameof(palette));
var allHues = palette.PrimarySwatch.PrimaryHues.ToList();
ReplacePrimaryColor(
palette.PrimarySwatch,
allHues[palette.PrimaryLightHueIndex],
allHues[palette.PrimaryMidHueIndex],
allHues[palette.PrimaryDarkHueIndex],
allHues);
var accentHue = palette.AccentSwatch.AccentHues.ElementAt(palette.AccentHueIndex);
ReplaceEntry("SecondaryAccentBrush", new SolidColorBrush(accentHue.Color));
ReplaceEntry("SecondaryAccentForegroundBrush", new SolidColorBrush(accentHue.Foreground));
后面还有 216 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明
AI 解读
登录后可用,每次 10 积分,解读结果公开显示在下面。
还没有人解读过这个文件。
