對于WPF動態換膚研究方案
WPF工具幫助開發人員簡單的實現了一些制作精美圖形界面的功能需求。在WPF動態換膚異常方便,只有將窗口資源設置為不同的ResourceDictionary就可以了。而且可以換得很徹底,甚至是徹底改變整個窗口上控件的種類,大小,個數等。#t#
下面是一個WPF動態換膚實現的方法。
建立一個叫做Blue.xaml的文件,在上面寫入
- < ResourceDictionary xmlns=
"http://schemas.microsoft.com
/winfx/2006/xaml/presentation" - xmlns:x="http://schemas.microsoft
.com/winfx/2006/xaml" - >
- < StackPanel x:Key="Root" Height="120">
- < Button Background="Blue" Height="40"/>
- < Button Background="Blue" Height="40"/>
- < Button Background="Blue" Height="40"/>
- < /StackPanel>
- < /ResourceDictionary>
然后建立一個叫Green.xaml的文件,在上面寫入
- < ResourceDictionary xmlns="http:
//schemas.microsoft.com/winfx/2006
/xaml/presentation"- xmlns:x="http://schemas.microsoft.
com/winfx/2006/xaml"- >
- < Grid x:Key="Root" Width="170"
Height="90">- < Grid.ColumnDefinitions>
- < ColumnDefinition Width="80"/>
- < ColumnDefinition Width="80"/>
- < /Grid.ColumnDefinitions>
- < Grid.RowDefinitions>
- < RowDefinition Height="40"/>
- < RowDefinition Height="40"/>
- < /Grid.RowDefinitions>
- < Button Background="Green"
Grid.Column="0" Grid.Row="0"/>- < Button Background="Green"
Grid.Column="1" Grid.Row="0"/>- < Button Background="Green"
Grid.Column="0" Grid.Row="1"/>- < Button Background="Green"
Grid.Column="1" Grid.Row="1"/>- < /Grid>
- < /ResourceDictionary>
然后是主窗口的xaml
- < Window x:Class="SkinTest2.
Window1"- xmlns="http://schemas.microsoft
.com/winfx/2006/xaml/presentation"- xmlns:x="http://schemas.microsoft
.com/winfx/2006/xaml"- Title="Window1" Height="150"
Width="300" Content="{Dynamic
Resource Root}">- < /Window>
將窗口的Content設置為一個動態資源Root就行了。
添加一個新類
Blue,在它的構造函數中設置將Blue.xaml中的ResourceDictionary設置給窗口
- ResourceDictionary resDic =
new ResourceDictionary();- resDic.Source = new Uri
("Blue.xaml", UriKind.Relative);- window.Resources = resDic;
StackPanel stackPanel = window.Content as StackPanel;
通過轉型來得到Blue中的StackPanel, 這種WPF動態換膚方法看起來有的粗魯,但是沒有想到別的辦法
- for (int i = 0; i
< stackPanel.Children.
Count; i++)- {
- button[i] = stackPanel.
Children[i] as Button;- button[i].Click +=
handler[i];- }
遍歷stackPanel的子元素,把Button一個個地取出來添加事件。沒法子。在寫Blue.xaml中的ResourceDictionary的時候不能給資源StackPanel的子元素再設置x:key了
添加一個Green類,同樣這么干。
***測試一下,在主窗口中放入一個托盤按鈕,方便一會切WPF動態換膚
- private Blue blue;
- private Green green;
- private System.Windows.
Forms.NotifyIcon notifyIcon;- }
- public Window1()
- {
- InitializeComponent();
- notifyIcon = new System.
Windows.Forms.NotifyIcon();- notifyIcon.Icon = Properties.
Resources.icon2;- System.Windows.Forms.
ContextMenu contextMenu =
new System.Windows.Forms.
ContextMenu();
給contextMenu添加兩個菜單項
- contextMenu.MenuItems
.Add("Blue").Click +=- ((sender, e) =>
- {
- if (blue == null)
- {
- blue = new Blue(this);
- green = null;
- }
- });
- contextMenu.MenuItems.
Add("green").Click +=- ((sender, e) =>
- {
- if(green == null)
- {
- green = new Green(this);
- blue = null;
- }
- });
這里用了3.0中的Lambda表達式,看起來還不賴,比起boost中的那個類庫級的Lambda看起來自然多了。
- notifyIcon.ContextMenu
= contextMenu;- notifyIcon.Visible
= true;
右擊托盤圖標,可以任意切換。當然WPF動態換膚換得這么徹底也很少見,都換了,和新建一個窗口有啥區別