對(duì)于WPF動(dòng)態(tài)換膚研究方案
WPF工具幫助開(kāi)發(fā)人員簡(jiǎn)單的實(shí)現(xiàn)了一些制作精美圖形界面的功能需求。在WPF動(dòng)態(tài)換膚異常方便,只有將窗口資源設(shè)置為不同的ResourceDictionary就可以了。而且可以換得很徹底,甚至是徹底改變整個(gè)窗口上控件的種類,大小,個(gè)數(shù)等。#t#
下面是一個(gè)WPF動(dòng)態(tài)換膚實(shí)現(xiàn)的方法。
建立一個(gè)叫做Blue.xaml的文件,在上面寫(xiě)入
- < 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>
然后建立一個(gè)叫Green.xaml的文件,在上面寫(xiě)入
- < 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設(shè)置為一個(gè)動(dòng)態(tài)資源Root就行了。
添加一個(gè)新類
Blue,在它的構(gòu)造函數(shù)中設(shè)置將Blue.xaml中的ResourceDictionary設(shè)置給窗口
- ResourceDictionary resDic =
new ResourceDictionary();- resDic.Source = new Uri
("Blue.xaml", UriKind.Relative);- window.Resources = resDic;
StackPanel stackPanel = window.Content as StackPanel;
通過(guò)轉(zhuǎn)型來(lái)得到Blue中的StackPanel, 這種WPF動(dòng)態(tài)換膚方法看起來(lái)有的粗魯,但是沒(méi)有想到別的辦法
- for (int i = 0; i
< stackPanel.Children.
Count; i++)- {
- button[i] = stackPanel.
Children[i] as Button;- button[i].Click +=
handler[i];- }
遍歷stackPanel的子元素,把Button一個(gè)個(gè)地取出來(lái)添加事件。沒(méi)法子。在寫(xiě)B(tài)lue.xaml中的ResourceDictionary的時(shí)候不能給資源StackPanel的子元素再設(shè)置x:key了
添加一個(gè)Green類,同樣這么干。
***測(cè)試一下,在主窗口中放入一個(gè)托盤(pán)按鈕,方便一會(huì)切WPF動(dòng)態(tài)換膚
- 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添加兩個(gè)菜單項(xiàng)
- 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表達(dá)式,看起來(lái)還不賴,比起boost中的那個(gè)類庫(kù)級(jí)的Lambda看起來(lái)自然多了。
- notifyIcon.ContextMenu
= contextMenu;- notifyIcon.Visible
= true;
右擊托盤(pán)圖標(biāo),可以任意切換。當(dāng)然WPF動(dòng)態(tài)換膚換得這么徹底也很少見(jiàn),都換了,和新建一個(gè)窗口有啥區(qū)別