1 Star 0 Fork 0

摩月流云/MaterialDesignInXamlToolkit

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ThemeAssist.cs 2.22 KB
一键复制 编辑 原始数据 按行查看 历史
Kevin B 提交于 2018-09-10 00:50 +08:00 . Fixing potential null reference exception. (#1037)
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;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/Jobc/MaterialDesignInXamlToolkit.git
git@gitee.com:Jobc/MaterialDesignInXamlToolkit.git
Jobc
MaterialDesignInXamlToolkit
MaterialDesignInXamlToolkit
master

搜索帮助