wpf 切换资源字典的2中方式
2024-08-27 23:26:54
var _1200RDUri = new Uri(String.Format(@"/aa;Component/Themes/1200Theme.xaml"), UriKind.RelativeOrAbsolute);
var _980RDUri = new Uri(String.Format(@"/aa;Component/Themes/980Theme.xaml"), UriKind.RelativeOrAbsolute);
var _1200RD = Application.LoadComponent(_1200RDUri) as ResourceDictionary;
var _980RD = Application.LoadComponent(_980RDUri) as ResourceDictionary;
if (_1200RD == null || _980RD == null)
{
return;
}
if (!IsScreenWidthAbove1200)
{
Application.Current.Resources.MergedDictionaries.Remove(_1200RD);
Application.Current.Resources.MergedDictionaries.Remove(_980RD);
Application.Current.Resources.MergedDictionaries.Add(_980RD);
}
else
{
Application.Current.Resources.MergedDictionaries.Remove(_1200RD);
Application.Current.Resources.MergedDictionaries.Remove(_980RD);
Application.Current.Resources.MergedDictionaries.Add(_1200RD);
}
private static void ChangeAppStyle(ResourceDictionary resources, Tuple<AppTheme, Accent> oldThemeInfo, Accent newAccent, AppTheme newTheme)
{
var themeChanged = false;
if (oldThemeInfo != null)
{
var oldAccent = oldThemeInfo.Item2;
if (oldAccent != null && oldAccent.Name != newAccent.Name)
{
var key = oldAccent.Resources.Source.ToString().ToLower();
var oldAccentResource = resources.MergedDictionaries.Where(x => x.Source != null).FirstOrDefault(d => d.Source.ToString().ToLower() == key);
if (oldAccentResource != null)
{
resources.MergedDictionaries.Add(newAccent.Resources);
resources.MergedDictionaries.Remove(oldAccentResource); themeChanged = true;
}
} var oldTheme = oldThemeInfo.Item1;
if (oldTheme != null && oldTheme != newTheme)
{
var key = oldTheme.Resources.Source.ToString().ToLower();
var oldThemeResource = resources.MergedDictionaries.Where(x => x.Source != null).FirstOrDefault(d => d.Source.ToString().ToLower() == key);
if (oldThemeResource != null)
{
resources.MergedDictionaries.Add(newTheme.Resources);
resources.MergedDictionaries.Remove(oldThemeResource); themeChanged = true;
}
}
}
else
{
ChangeAppStyle(resources, newAccent, newTheme); themeChanged = true;
} if (themeChanged)
{
OnThemeChanged(newAccent, newTheme);
}
}
这段不能直接用,得看一下代码意思 Application.Current.Resources
Resources = new ResourceDictionary {Source = resourceAddress};
最新文章
- C# Redis Server分布式缓存编程 --网络转载
- Wordpress如何开启用户注册功能
- C#集合-列举(Enumeration)
- HDU 4883 TIANKENG’s restaurant
- 清除行内元素之间HTML空白的几种解决方案
- control file sequential read 等待事件
- 计算机管理cmd命令行
- java选择排序详解
- Dynamics 365 Online-60000 API Limits
- Python 内置方法
- Day2 《机器学习》第二章学习笔记
- Lavarel Route::resource
- 基于【字节】操作的IO接口:InputStream、OutputStream
- 使用mysqlproxy实现mysql读写分离
- 百度分享不支持Https的解决方案--本地化
- ubuntu软件安装位置
- 微信小程序开发4之form表单与弹出层
- 《Gradle权威指南》--Android Gradle插件
- sublime text3 增加代码片段(snipper)
- CSS基础-DAY2
热门文章
- gvim 安装YCM
- 【bzoj1606】[Usaco2008 Dec]Hay For Sale 购买干草
- 用VC2010以上版本编译可以在低版本XP和2003的运行程序的方法
- Java性能调优之:idea变慢解决
- JS判断字符串长度(中文长度为2,英文长度为1)
- &;&;&;&;数组去重方法总结&;&;&;&;&;
- This is US 我们的生活
- vs写python扩展资料收集
- getComputedStyle/currentStyle/style之间的爱恨情仇
- Linux sort 命令