对于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动态换肤换得这么彻底也很少见,都换了,和新建一个窗口有啥区别

THE END