博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF之动态换肤
阅读量:6685 次
发布时间:2019-06-25

本文共 4369 字,大约阅读时间需要 14 分钟。

原文:

如何实现换肤呢,对于复杂的换肤操作,如,更换按钮样式、窗口样式等,我们需要写多个资源字典来表示不同的皮肤,通过动态加载不同的资源字典来实现换肤的效果;对于简单的换肤操作,如更改背景颜色、设置窗体透明度,这种换肤操作,我们就不能使用上面的方法了,这个时候,我们只要在一个全局对象中添加几个属性,如背景颜色、前景颜色、窗体透明度等,然后,再绑定这几个属性就能达到我们想要的效果。

 

解决方案:动态加载资源字典

1 
5
6
7
8
9
12
22 23
24
25
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace DynamicallySkinnable{    ///     /// MainWindow.xaml 的交互逻辑    ///     public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void Button_Click(object sender, RoutedEventArgs e)        {            Button btn = (Button)e.OriginalSource;            btn.ContextMenu.IsOpen = true;        }        private void menuAero_Click(object sender, RoutedEventArgs e)        {            /*            Application.Current.Resources.MergedDictionaries.Clear();   //清除现有资源            //获取要应用的资源字典            ResourceDictionary resource =                (ResourceDictionary)Application.LoadComponent(                    new Uri("/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;component/themes/aero.normalcolor.xaml", UriKind.Relative));            //将资源字典合并到当前资源中            Application.Current.Resources.MergedDictionaries.Add(resource);             */            //this.Resources.MergedDictionaries.Clear();            ResourceDictionary resource = (ResourceDictionary)Application.LoadComponent(new Uri("/DynamicallySkinnable;component/Skins/BlueSkin.xaml", UriKind.Relative));            this.Resources.MergedDictionaries.Add(resource);        }        private void menuRoyale_Click(object sender, RoutedEventArgs e)        {            /*            Application.Current.Resources.MergedDictionaries.Clear();            ResourceDictionary resource =                (ResourceDictionary)Application.LoadComponent(                    new Uri("/PresentationFramework.Royale, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;component/themes/royale.normalcolor.xaml", UriKind.Relative));            Application.Current.Resources.MergedDictionaries.Add(resource);             */            //this.Resources.MergedDictionaries.Clear();            ResourceDictionary resource = (ResourceDictionary)Application.LoadComponent(new Uri("/DynamicallySkinnable;component/Skins/RedSkin.xaml", UriKind.Relative));            this.Resources.MergedDictionaries.Add(resource);                   }        private void menuLuna_Click(object sender, RoutedEventArgs e)        {            /*            Application.Current.Resources.MergedDictionaries.Clear();            ResourceDictionary resource =                (ResourceDictionary)Application.LoadComponent(                    new Uri("/PresentationFramework.Luna, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;component/themes/luna.normalcolor.xaml", UriKind.Relative));            Application.Current.Resources.MergedDictionaries.Add(resource);             */            //this.Resources.MergedDictionaries.Clear();            ResourceDictionary resource = (ResourceDictionary)Application.LoadComponent(new Uri("/DynamicallySkinnable;component/Skins/BlackSkin.xaml", UriKind.Relative));            this.Resources.MergedDictionaries.Add(resource);        }        private void button1_Click(object sender, RoutedEventArgs e)        {            Button btn = (Button)e.OriginalSource;            btn.ContextMenu.IsOpen = true;        }    }}

 

后记:

  动态换肤在程序里面已经基本实现,主要是资源字典的动态加载,以及背景图片的替换,在Grid.Background的ImageBrush属性里面,在点击按钮之后更换了资源字典之后还是需要手动写代码替换一下背景的

写的时候就在想能不能写成属性通知的那个样子,当它发生改变了,自动去更新,不用我手动的去写代码,但是有种无从下手的感觉。

  这段时间老是会觉得自己的知识不够,遇到问题不能从本质上去了解及解决,一定要在网上荡代码参考怎么样的,实用主义,有的时候是好,但是后期的积累问题会越来越难以解决,所以要注意夯实基础,这段时间

需要好好的看一看基础的组成什么的。感觉自己什么都很匮乏。代码写的不够优雅,自己看着都觉得凌乱,变量名不优美,看着不赏心悦目。这些都是我的目标,现在方向有了。向着我的程序员之路,fighting!

转载地址:http://uxiao.baihongyu.com/

你可能感兴趣的文章
All Things OpenTSDB
查看>>
android 网络通信框架volly
查看>>
二分查找算法及其变种
查看>>
一个泛型冒泡排序的实现
查看>>
大型分布式网站架构设计与实践 第一章《面向服务的体系架构(SOA)》
查看>>
[From OpenBSD Man Page]PFSYNC
查看>>
hdu 5131 Song Jiang's rank list 【2014ACM/ICPC亚洲区广州站-重现赛】
查看>>
JS笔记(20): JS中的同步编程和异步编程
查看>>
那几个题(没懂的地方留言)
查看>>
如何改变UITableViewCell的选中样式(颜色)?storyboard上cell的selection不可用?
查看>>
Ubuntu 怎么增加根目录 大小
查看>>
Spring Cloud微服务分布式云架构—集成项目简介
查看>>
盒马鲜生颠覆传统生鲜市场的胜算几何?
查看>>
【Node】常用基础 API 整理
查看>>
传神成进博会唯一指定智能翻译硬件提供商 力助无障碍沟通
查看>>
微信小程序实现slideUp、slideDown滑动效果及点击空白隐藏功能示例
查看>>
Java程序员须知:分布式微服务为什么很难?
查看>>
SQLServer之创建唯一聚集索引
查看>>
好程序员web前端技术之CSS3过渡
查看>>
java B2B2C源码电子商务平台 - Zuul回退机制
查看>>