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 Clock to 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. <Path x:Name="HourHandPath" Data="M 0 15 L 10 0, 0 -60, -10 0 Z" Fill="{DynamicResource HourHandFillBrush}" Stroke="{DynamicResource ClockHandsBrush}"> … </Path> These resources are declared inside of a Resource Dictionary: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="MainWindowBackgroundBrush" Color="#FFFFFFFF"/> <SolidColorBrush x:Key="ClockHandsBrush" Color="#FF000000"/> <SolidColorBrush x:Key="TicksStrokeBrush" Color="#FF000000"/> <SolidColorBrush x:Key="HourHandFillBrush" Color="#FFA0A0A0"/> <SolidColorBrush x:Key="MinuteHandFillBrush" Color="#FFE3E3E3"/> <SolidColorBrush x:Key="TextForegroundBrush" Color="#FF000000"/>
… </ResourceDictionary> The resource dictionary is then merged to be use by the application in App.xaml as follow: <Application 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"> <Application.Resources> <!-- Resources scoped at the Application level should be defined here. --> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Skins\Default\DefaultResources.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> 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. private void SkinsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ResourceDictionary rd; switch ((Skin)((ComboBox)sender).SelectedItem) { case Skin.Dark: rd = Application.LoadComponent(new Uri(@"Skins/Dark/DarkResources.xaml", UriKind.Relative)) as ResourceDictionary; break; case Skin.Triangle: rd = Application.LoadComponent(new Uri(@"Skins/Triangle/TriangleResources.xaml", UriKind.Relative)) as ResourceDictionary; break; case Skin.Blue: rd = Application.LoadComponent(new Uri(@"Skins/Blue/BlueResources.xaml", UriKind.Relative)) as ResourceDictionary; break; default: rd = Application.LoadComponent(new Uri(@"Skins/Default/DefaultResources.xaml", UriKind.Relative)) as ResourceDictionary; break; } Application.Current.Resources = rd; } You can try the XBAP here: WPF Skinnable Clock XBAP You can download the source here: WPF Skinning |
WPF 换肤
2008-07-10 17:17
转载于:https://www.cnblogs.com/coogle/archive/2008/07/18/1245677.html