对于WPF动态换肤研究方案

开发 开发工具
WPF动态换肤的方法在实际开发中是经常会遇到的一个情况。我们需要牢固掌握这一技巧,以方便提高我们的开发效率。

WPF工具帮助开发人员简单的实现了一些制作精美图形界面的功能需求。在WPF动态换肤异常方便,只有将窗口资源设置为不同的ResourceDictionary就可以了。而且可以换得很彻底,甚至是彻底改变整个窗口上控件的种类,大小,个数等。#t#

下面是一个WPF动态换肤实现的方法。
建立一个叫做Blue.xaml的文件,在上面写入

  1. < ResourceDictionary xmlns=
    "http://schemas.microsoft.com
    /winfx/2006/xaml/presentation"
     
  2. xmlns:x="http://schemas.microsoft
    .com/winfx/2006/xaml"
     
  3. > 
  4. < StackPanel x:Key="Root" Height="120"> 
  5. < Button Background="Blue" Height="40"/> 
  6. < Button Background="Blue" Height="40"/> 
  7. < Button Background="Blue" Height="40"/> 
  8. < /StackPanel> 
  9. < /ResourceDictionary> 

然后建立一个叫Green.xaml的文件,在上面写入

  1. < ResourceDictionary xmlns="http:
    //schemas.microsoft.com/winfx/2006
    /xaml/presentation"
     
  2. xmlns:x="http://schemas.microsoft.
    com/winfx/2006/xaml"
     
  3. > 
  4. < Grid x:Key="Root" Width="170" 
    Height="90"> 
  5. < Grid.ColumnDefinitions>   
  6. < ColumnDefinition Width="80"/> 
  7. < ColumnDefinition Width="80"/> 
  8. < /Grid.ColumnDefinitions> 
  9. < Grid.RowDefinitions> 
  10. < RowDefinition Height="40"/> 
  11. < RowDefinition Height="40"/> 
  12. < /Grid.RowDefinitions> 
  13. < Button Background="Green" 
    Grid.Column="0" Grid.Row="0"/> 
  14. < Button Background="Green" 
    Grid.Column="1" Grid.Row="0"/> 
  15. < Button Background="Green" 
    Grid.Column="0" Grid.Row="1"/> 
  16. < Button Background="Green" 
    Grid.Column="1" Grid.Row="1"/> 
  17. < /Grid> 
  18. < /ResourceDictionary> 

然后是主窗口的xaml

  1. < Window x:Class="SkinTest2.
    Window1"
     
  2. xmlns="http://schemas.microsoft
    .com/winfx/2006/xaml/presentation"
     
  3. xmlns:x="http://schemas.microsoft
    .com/winfx/2006/xaml"
     
  4. Title="Window1" Height="150" 
    Width="300" Content="{Dynamic
    Resource Root}"
    > 
  5. < /Window> 

将窗口的Content设置为一个动态资源Root就行了。
添加一个新类
Blue,在它的构造函数中设置将Blue.xaml中的ResourceDictionary设置给窗口

  1. ResourceDictionary resDic =
     
    new ResourceDictionary();  
  2. resDic.Source = new Uri
    ("Blue.xaml", UriKind.Relative);  
  3. window.Resources = resDic

StackPanel stackPanel = window.Content as StackPanel;
通过转型来得到Blue中的StackPanel, 这种WPF动态换肤方法看起来有的粗鲁,但是没有想到别的办法

  1. for (int i = 0; i 
    <  stackPanel.Children.
    Count
    ; i++)  
  2. {  
  3. button[i] = stackPanel.
    Children[i] as Button;  
  4. button[i].Click += 
    handler[i];  

遍历stackPanel的子元素,把Button一个个地取出来添加事件。没法子。在写Blue.xaml中的ResourceDictionary的时候不能给资源StackPanel的子元素再设置x:key了
添加一个Green类,同样这么干。

***测试一下,在主窗口中放入一个托盘按钮,方便一会切WPF动态换肤

  1. private Blue blue;  
  2. private Green green;  
  3. private System.Windows.
    Forms.NotifyIcon notifyIcon;  
  4. }  
  5. public Window1()  
  6. {  
  7. InitializeComponent();  
  8. notifyIcon = new System.
    Windows.Forms.NotifyIcon();  
  9. notifyIcon.Icon = Properties.
    Resources.icon2;  
  10. System.Windows.Forms.
    ContextMenu 
    contextMenu = 
    new System.Windows.Forms.
    ContextMenu(); 

给contextMenu添加两个菜单项

  1. contextMenu.MenuItems
    .Add("Blue").Click +=  
  2. ((sender, e) =>   
  3. {   
  4. if (blue == null)  
  5. {   
  6. blue = new Blue(this);   
  7. green = null;   
  8. }   
  9. }); 

  1. contextMenu.MenuItems.
    Add("green").Click +=   
  2. ((sender, e) =>   
  3. {  
  4. if(green == null)  
  5. {   
  6. green = new Green(this);   
  7. blue = null;   
  8. }  
  9. }); 

这里用了3.0中的Lambda表达式,看起来还不赖,比起boost中的那个类库级的Lambda看起来自然多了。

  1. notifyIcon.ContextMenu 
    contextMenu;  
  2. notifyIcon.Visible 
    true;  

右击托盘图标,可以任意切换。当然WPF动态换肤换得这么彻底也很少见,都换了,和新建一个窗口有啥区别

责任编辑:曹凯 来源: CSDN
相关推荐

2010-07-16 17:56:18

CMD Telnet

2010-08-05 15:05:44

NFS挂载

2010-01-15 15:13:28

C++概述

2010-02-03 15:52:02

Python学习研究

2022-04-07 13:56:13

前端一键换肤

2009-12-23 10:35:55

WPF应用程序项目

2009-12-03 14:53:49

.NET Framew

2009-12-24 15:51:34

ADO属性

2009-12-04 17:56:18

WCF 服务测试

2009-12-23 14:30:24

WPF显示动态GIF

2010-07-15 10:24:25

Microsoft T

2010-06-21 13:07:14

2009-12-22 09:25:57

ADO.NET3.5

2010-07-08 13:00:30

动态路由协议

2009-12-28 16:45:31

WPF窗体

2009-12-25 17:10:51

WPF动态资源

2010-01-04 10:14:04

ADO.NET对象模型

2010-09-10 11:27:07

WCDMA核心网络协议栈

2010-09-02 10:50:55

CISCO路由器dhc

2023-10-04 18:24:54

wpf动态资源
点赞
收藏

51CTO技术栈公众号