1. 程式人生 > >WPF中的Generic.xaml, theme以及custom control

WPF中的Generic.xaml, theme以及custom control

question value efi onf dde for ng- then ati

原文:WPF中的Generic.xaml, theme以及custom control

在Visual Studio中創建自定義控件時,所有控件都將添加到/Themes/Generic.xaml

最近做的項目用了個漂亮的開源UI庫,結果項目臨近結尾發現要支持.Net 3.5, 然而那個UI庫卻最低支持4.0。欲哭無淚,最後決定拿掉那個庫,自己改改style得了。深刻的教訓。

作為程序員一般都比較害怕搞界面,這下硬著頭皮上,折騰折騰Blend,抄抄改改各種style,弄著弄著居然能看懂個大概了。最後在自己建立的UI庫項目中如何組織資源有了一些總結,進入正題:

“Generic.xaml” 早有耳聞,不清楚究竟有什麽作用。這次Google之,stackoverflow上早已有人發問。What is so special about Generic.xaml?

第一個答案清晰明了。

Every Control in WPF has a default Style that provides, among other things, the Control‘s default ControlTemplate. WPF looks for the default style in a special resource dictionary in the Themes folder in the same assembly as the control. The key for the default style is provided by the Control.DefaultStyleKey

dependency property, the default value of which is overridden in each sub-class of Control.

The name of the resource dictionary depends on the current Windows theme e.g. on Vista using the Aero theme, the dictionary is called Aero.NormalColor.xaml, on XP using the default theme it is Luna.NormalColor.xaml. If the style is not found in the theme dictionary, it looks in Generic.xaml i.e for controls whose look doesn‘t depend on the theme.

This only applies to any custom controls you have defined i.e. classes derived from Control, directly or indirectly. You can change the default style for a standard control by deriving from it and calling DefaultStyleKeyProperty.OverrideMetadata in the static constructor, but you then have to supply the full style including ControlTemplate.

Note that you can tell WPF to look in an external assembly for your default style by using the ThemeInfo attribute. The external assembly must be named <YourAssembly>.<ThemeName>.dll e.g. PresententationFramework.Aero.dll.

講解了Generic.xaml的來龍去脈。順便提了如何使用DefaultStyleKeyProperty.OverrideMetadata 來改變WPF查找一個control的默認style時用的類型。還提到了 ThemeInfo.

下面這篇文章,Structuring Your XAML Sources,代碼演示的非常清晰。通過它還能了解創建一個Custom Control的典型做法,很有幫助。總體來說,Generic.xaml在UI Library中的好處就是,使用這個library的項目不用再引用library的resource dictionary,UI Library中定義的custom control也能自動地找到它的默認style。

看文章附帶的源代碼,以及開源庫的代碼,發現它們都在AssemblyInfo.cs中有ThemeInfo. 這是必不可少的。

[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

WPF中的Generic.xaml, theme以及custom control