导读:
Yesterday, I wrote about the differences between theming and skinning in WPF. Today, I want to show how I plan to implement skinning a WPF application.
The general idea for skinning is to replace the application resource dictionary, which contains the styles, brushes, and templates, based on the user's input. In this case, the user can select a skin from a combo box. I will be using Petzold's excellent XAML Clockto demonstrate.
Here is a screenshot of my skinnable XAML Clock.
Here it is again with the Dark skin applied. I included four generic skins in this sample.
In order to change the clock hands and tick marks, I apply the stroke and fill properties to dynamic resources. Here's the XAML for the Hour hand. Note that the Fill and Stroke properties are set to dynamic resources.
????Fill< Fill = " {DynamicResource HourHandFillBrush} " Stroke = " {DynamicResource ClockHandsBrush} ">
…
These resources are declared inside of a Resource Dictionary:
????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
< SolidColorBrush x:Key = " ClockHandsBrush " Color = " #FF000000 " />
< SolidColorBrush x:Key = " HourHandFillBrush " Color = " #FFA0A0A0 " />
…
The resource dictionary is then merged to be use by the application in App.xaml as follow:
????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
????x:Class="WPFSkinning.App"
????StartupUri="Window1.xaml"> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WPFSkinning.App"
StartupUri="Window1.xaml">
< ResourceDictionary >
< ResourceDictionary.MergedDictionaries >
< ResourceDictionary Source = " Skins/Default/DefaultResources.xaml " />
ResourceDictionary.MergedDictionaries>
ResourceDictionary>
I organized each of the skin resources into their own resource dictionaries in separate folders.
To change the skin based on the combo box, I have an event handler for the selection changed event. The skins are loaded with Application.LoadComponent which makes them objects. The application resources are then changed to the new resource dictionary for the corresponding skin.
privatevoidSkinsComboBox_SelectionChanged(objectsender, SelectionChangedEventArgse)
{
ResourceDictionaryrd;
switch((Skin)((ComboBox)sender).SelectedItem)
{
caseSkin.Dark:
rd = Application.LoadComponent(newUri(@"Skins/Dark/DarkResources.xaml", UriKind.Relative)) asResourceDictionary
break
caseSkin.Triangle:
rd = Application.LoadComponent(newUri(@"Skins/Triangle/TriangleResources.xaml", UriKind.Relative)) asResourceDictionary
break
caseSkin.Blue:
rd = Application.LoadComponent(newUri(@"Skins/Blue/BlueResources.xaml", UriKind.Relative)) asResourceDictionary
break
default:
rd = Application.LoadComponent(newUri(@"Skins/Default/DefaultResources.xaml", UriKind.Relative)) asResourceDictionary
break
}
Application.Current.Resources = rd;
}
You can try the XBAP here: WPF Skinnable Clock XBAP
You can download the source here: WPF Skinning
Posted at 2:40 PM by Alan Le | Permalink| Email this Post| Comments (10)
Edit in Browser /_layouts/images/icxddoc.gif /personal/alanl/Blog/_layouts/formserver.aspx?XsnLocation={ItemUrl}&OpenIn=Browser 0x0 0x1 FileType xsn 255
Edit in Browser /_layouts/images/icxddoc.gif /personal/alanl/Blog/_layouts/formserver.aspx?XmlLocation={ItemUrl}&OpenIn=Browser 0x0 0x1 ProgId InfoPath.Document 255
Edit in Browser /_layouts/images/icxddoc.gif /personal/alanl/Blog/_layouts/formserver.aspx?XmlLocation={ItemUrl}&OpenIn=Browser 0x0 0x1 ProgId InfoPath.Document.2 255
Edit in Browser /_layouts/images/icxddoc.gif /personal/alanl/Blog/_layouts/formserver.aspx?XmlLocation={ItemUrl}&OpenIn=Browser 0x0 0x1 ProgId InfoPath.Document.3 255
Edit in Browser /_layouts/images/icxddoc.gif /personal/alanl/Blog/_layouts/formserver.aspx?XmlLocation={ItemUrl}&OpenIn=Browser 0x0 0x1 ProgId InfoPath.Document.4 255
View in Web Browser /_layouts/images/ichtmxls.gif /personal/alanl/Blog/_layouts/xlviewer.aspx?listguid={ListId}&itemid={ItemId}&DefaultItemOpen=1 0x0 0x1 FileType xlsx 255
View in Web Browser /_layouts/images/ichtmxls.gif /personal/alanl/Blog/_layouts/xlviewer.aspx?listguid={ListId}&itemid={ItemId}&DefaultItemOpen=1 0x0 0x1 FileType xlsb 255
Snapshot in Excel /_layouts/images/ewr134.gif /personal/alanl/Blog/_layouts/xlviewer.aspx?listguid={ListId}&itemid={ItemId}&Snapshot=1 0x0 0x1 FileType xlsx 256
Snapshot in Excel /_layouts/images/ewr134.gif /personal/alanl/Blog/_layouts/xlviewer.aspx?listguid={ListId}&itemid={ItemId}&Snapshot=1 0x0 0x1 FileType xlsb 256
本文转自
http://blogs.vertigo.com/personal/alanl/Blog/Lists/Posts/Post.aspx?List=d8ffd567%2Ddf86%2D4de5%2D9b5a%2D00fd37f58cda&ID=15
Yesterday, I wrote about the differences between theming and skinning in WPF. Today, I want to show how I plan to implement skinning a WPF application.
The general idea for skinning is to replace the application resource dictionary, which contains the styles, brushes, and templates, based on the user's input. In this case, the user can select a skin from a combo box. I will be using Petzold's excellent XAML Clockto demonstrate.
Here is a screenshot of my skinnable XAML Clock.
Here it is again with the Dark skin applied. I included four generic skins in this sample.
In order to change the clock hands and tick marks, I apply the stroke and fill properties to dynamic resources. Here's the XAML for the Hour hand. Note that the Fill and Stroke properties are set to dynamic resources.
????Fill< Fill = " {DynamicResource HourHandFillBrush} " Stroke = " {DynamicResource ClockHandsBrush} ">
…
These resources are declared inside of a Resource Dictionary:
????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
< SolidColorBrush x:Key = " ClockHandsBrush " Color = " #FF000000 " />
< SolidColorBrush x:Key = " HourHandFillBrush " Color = " #FFA0A0A0 " />
…
The resource dictionary is then merged to be use by the application in App.xaml as follow:
????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
????x:Class="WPFSkinning.App"
????StartupUri="Window1.xaml"> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WPFSkinning.App"
StartupUri="Window1.xaml">
< ResourceDictionary >
< ResourceDictionary.MergedDictionaries >
< ResourceDictionary Source = " Skins/Default/DefaultResources.xaml " />
ResourceDictionary.MergedDictionaries>
ResourceDictionary>
I organized each of the skin resources into their own resource dictionaries in separate folders.
To change the skin based on the combo box, I have an event handler for the selection changed event. The skins are loaded with Application.LoadComponent which makes them objects. The application resources are then changed to the new resource dictionary for the corresponding skin.
privatevoidSkinsComboBox_SelectionChanged(objectsender, SelectionChangedEventArgse)
{
ResourceDictionaryrd;
switch((Skin)((ComboBox)sender).SelectedItem)
{
caseSkin.Dark:
rd = Application.LoadComponent(newUri(@"Skins/Dark/DarkResources.xaml", UriKind.Relative)) asResourceDictionary
break
caseSkin.Triangle:
rd = Application.LoadComponent(newUri(@"Skins/Triangle/TriangleResources.xaml", UriKind.Relative)) asResourceDictionary
break
caseSkin.Blue:
rd = Application.LoadComponent(newUri(@"Skins/Blue/BlueResources.xaml", UriKind.Relative)) asResourceDictionary
break
default:
rd = Application.LoadComponent(newUri(@"Skins/Default/DefaultResources.xaml", UriKind.Relative)) asResourceDictionary
break
}
Application.Current.Resources = rd;
}
You can try the XBAP here: WPF Skinnable Clock XBAP
You can download the source here: WPF Skinning
Posted at 2:40 PM by Alan Le | Permalink| Email this Post| Comments (10)
Edit in Browser /_layouts/images/icxddoc.gif /personal/alanl/Blog/_layouts/formserver.aspx?XsnLocation={ItemUrl}&OpenIn=Browser 0x0 0x1 FileType xsn 255
Edit in Browser /_layouts/images/icxddoc.gif /personal/alanl/Blog/_layouts/formserver.aspx?XmlLocation={ItemUrl}&OpenIn=Browser 0x0 0x1 ProgId InfoPath.Document 255
Edit in Browser /_layouts/images/icxddoc.gif /personal/alanl/Blog/_layouts/formserver.aspx?XmlLocation={ItemUrl}&OpenIn=Browser 0x0 0x1 ProgId InfoPath.Document.2 255
Edit in Browser /_layouts/images/icxddoc.gif /personal/alanl/Blog/_layouts/formserver.aspx?XmlLocation={ItemUrl}&OpenIn=Browser 0x0 0x1 ProgId InfoPath.Document.3 255
Edit in Browser /_layouts/images/icxddoc.gif /personal/alanl/Blog/_layouts/formserver.aspx?XmlLocation={ItemUrl}&OpenIn=Browser 0x0 0x1 ProgId InfoPath.Document.4 255
View in Web Browser /_layouts/images/ichtmxls.gif /personal/alanl/Blog/_layouts/xlviewer.aspx?listguid={ListId}&itemid={ItemId}&DefaultItemOpen=1 0x0 0x1 FileType xlsx 255
View in Web Browser /_layouts/images/ichtmxls.gif /personal/alanl/Blog/_layouts/xlviewer.aspx?listguid={ListId}&itemid={ItemId}&DefaultItemOpen=1 0x0 0x1 FileType xlsb 255
Snapshot in Excel /_layouts/images/ewr134.gif /personal/alanl/Blog/_layouts/xlviewer.aspx?listguid={ListId}&itemid={ItemId}&Snapshot=1 0x0 0x1 FileType xlsx 256
Snapshot in Excel /_layouts/images/ewr134.gif /personal/alanl/Blog/_layouts/xlviewer.aspx?listguid={ListId}&itemid={ItemId}&Snapshot=1 0x0 0x1 FileType xlsb 256
本文转自
http://blogs.vertigo.com/personal/alanl/Blog/Lists/Posts/Post.aspx?List=d8ffd567%2Ddf86%2D4de5%2D9b5a%2D00fd37f58cda&ID=15