代码拉取完成,页面将自动刷新
using System;
using System.Windows;
namespace MaterialDesignThemes.Wpf
{
public static class ThemeAssist
{
public static BaseTheme GetTheme(DependencyObject obj)
{
return (BaseTheme)obj.GetValue(ThemeProperty);
}
public static void SetTheme(DependencyObject obj, BaseTheme value)
{
obj.SetValue(ThemeProperty, value);
}
public static readonly DependencyProperty ThemeProperty =
DependencyProperty.RegisterAttached("Theme", typeof(BaseTheme), typeof(ThemeAssist), new PropertyMetadata(default(BaseTheme), OnThemeChanged));
private static void OnThemeChanged(DependencyObject @do, DependencyPropertyChangedEventArgs e)
{
if (@do is FrameworkElement element)
{
if (e.OldValue is BaseTheme oldTheme &&
GetResourceDictionarySource(oldTheme) is string oldSource)
{
foreach(ResourceDictionary resourceDictionary in element.Resources.MergedDictionaries)
{
if (string.Equals(resourceDictionary.Source?.ToString(), oldSource, StringComparison.OrdinalIgnoreCase))
{
element.Resources.MergedDictionaries.Remove(resourceDictionary);
break;
}
}
}
if (e.NewValue is BaseTheme newTheme &&
GetResourceDictionarySource(newTheme) is string newThemeSource)
{
element.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri(newThemeSource) });
}
}
}
private static string GetResourceDictionarySource(BaseTheme theme)
{
switch (theme)
{
case BaseTheme.Light:
return "pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml";
case BaseTheme.Dark:
return "pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml";
}
return null;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。