Skinning a WPF Application

导读:
  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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值