Silverlight自定義控件管理二重奏
在Silverlight里面建自定義控件(Templated Control),會在工程下生成一個Themes文件夾,并在其中包含一個generic.xaml文件。這是一個ResourceDictionary文件,所有的自定義控件的默認樣式(Default Style)都必須放在這里。
最原始的辦法就是把所有樣式都直接寫在generic.xaml文件里,但如果自定義控件足夠多,generic.xaml 達到了好幾千行,管理起來當然十分麻煩。后來在同事的推薦下,得到兩種方法可以將各自定義控件的樣式分開管理,總算解決了這一令人頭疼的問題。
MergeDefaultStyle方法
如果研究過Silverlight Toolkit的源代碼,會發(fā)現(xiàn)里面所有的自定義控件都有一個單獨的xaml文件來保存控件的默認樣式,當然這些文件是不起作用的。最初以為是先用單獨的xaml文件來寫控件樣式,然后再拷貝到generic.xaml里,也就是人工同步。
然而現(xiàn)在發(fā)現(xiàn)MergeDefaultStyle方法。MergeDefaultStyle就是通過給所有單獨的xaml文件應用一種特殊的 Build 方法,在 Build 工程的時候,自動把 xaml 文件的內容整合到 generic.xaml 里去。
重點步驟是:
1. 拷貝里面的代碼或者直接下載MergeDefaultStyle.dll。
2. 在VS里面Unload你的工程,然后編輯工程文件,或者直接用文本編輯器打開csproj文件。
3. 在最后加上下面這段代碼:
- <UsingTask
- TaskName="Engineering.Build.Tasks.MergeDefaultStylesTask"
- AssemblyFile="$(EngineeringResources)\Engineering.Build.dll" />
注意:AssemblyFile 的值是你放MergeDefaultStyle.dll的位置,可以用相對路徑。
4. 再在后面加上這一段代碼:
- <!-- Add "DefaultStyle" as a Build Action in Visual Studio -->
- <ItemGroup Condition="'$(BuildingInsideVisualStudio)'=='true'">
- <AvailableItemName Include="DefaultStyle" />
- </ItemGroup>
- <!--
- Merge the default styles of controls
- (only if any of the DefaultStyle files is
- more recent than the project's generic.xaml file)
- before compilation
- dependencies are processed.
- -->
- <PropertyGroup>
- <PrepareResourcesDependsOn>
- MergeDefaultStyles;
- $(PrepareResourcesDependsOn);
- </PrepareResourcesDependsOn>
- </PropertyGroup>
- <Target
- Name="MergeDefaultStyles"
- Inputs="@(DefaultStyle)"
- Outputs="$(MSBuildProjectDirectory)\generic.xaml">
- <MergeDefaultStylesTask
- DefaultStyles="@(DefaultStyle)"
- ProjectDirectory="$(MSBuildProjectDirectory)" />
- </Target>
- <!--
- Touch DefaultStyles on Rebuild to force generation of generic.xaml.
- -->
- <PropertyGroup>
- <RebuildDependsOn>
- TouchDefaultStyles;
- $(RebuildDependsOn);
- </RebuildDependsOn>
- </PropertyGroup>
- <Target Name="TouchDefaultStyles">
- <Touch Files="@(DefaultStyle)" ForceTouch="true" />
- </Target>
5. 重新 Load 你的工程。
6. 選擇有默認樣式的單獨的xaml,在屬性窗口的 Build Action 里面選擇 DefaultStyle 。
7. 編譯整個工程,再打開generic.xaml文件,你會發(fā)現(xiàn) xaml 文件里的內容已經(jīng)拷到generic.xaml里面了。
這一方法適用于Silverlight 3及Silverlight 4 。
MergedDictionary方法
上面的方法可謂是一勞永逸了,但多少有點不官方。而且其實還是generic.xaml掌控全局,一旦一個xaml文件出了紕漏,會影響所有的控件跟著出錯。這樣排查起來也麻煩的很。
于是在Silverlight 3里就出來了一個更簡單更官方的方法。如前所述,generic.xaml文件包含了一個ResourceDictionary,而Silverlight 3里面的ResourceDictionary 多了一個MergedDictionaries的屬性,可以把其他ResourceDictionary通過資源路徑整合到一個ResourceDicionary里面。
其實新建一個Silverlight導航應用時,就可以在App.xaml 里面看到這一屬性的應用。需要注意的是,在 App.xaml 里面是可以用相對路徑的,而在 generic.xaml 里面,不可以用相對路徑,而應當用 "/AssemblyName;component/path”的方法說明文件路徑。
比如你的工程的AssemblyName是Slippor.Controls,而xaml的路徑是CustomControl文件夾下的CustomControl.xaml 。則應該在generic.xaml里面如下寫:
- <ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
- <ResourceDictionary.MergedDictionaries>
- <ResourceDictionary Source="/Slippor.Controls;component/CustomControl/CustomControl.xaml"/>
- </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>這一方法適用于Silverlight 3及Silverlight 4。
原文作者:smjack
原文地址:http://www.cnblogs.com/smjack/archive/2010/08/24/1807706.html
【編輯推薦】