WPF 换肤

WPF 换肤
2008-07-10 17:17

引: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 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

转载于:https://www.cnblogs.com/coogle/archive/2008/07/18/1245677.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值