自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Silverlight自定義控件管理二重奏

開發(fā) 后端
用Silverlight創(chuàng)建自定義控件,最原始的辦法就是把所有樣式都直接寫在generic.xaml文件里,但如果自定義控件足夠多,generic.xaml 達到了好幾千行,管理起來當然十分麻煩,這里給大家介紹兩種較為方便的管理方法。

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. 在最后加上下面這段代碼:

  1. <UsingTask 
  2.   TaskName="Engineering.Build.Tasks.MergeDefaultStylesTask" 
  3.   AssemblyFile="$(EngineeringResources)\Engineering.Build.dll" /> 

注意:AssemblyFile 的值是你放MergeDefaultStyle.dll的位置,可以用相對路徑。

4. 再在后面加上這一段代碼:

  1. <!-- Add "DefaultStyle" as a Build Action in Visual Studio --> 
  2. <ItemGroup Condition="'$(BuildingInsideVisualStudio)'=='true'"> 
  3.   <AvailableItemName Include="DefaultStyle" /> 
  4. </ItemGroup> 
  5. <!--  
  6. Merge the default styles of controls 
  7. (only if any of the DefaultStyle files is  
  8. more recent than the project's generic.xaml file)
  9. before compilation  
  10. dependencies are processed.  
  11. --> 
  12. <PropertyGroup> 
  13.   <PrepareResourcesDependsOn> 
  14.     MergeDefaultStyles;  
  15.     $(PrepareResourcesDependsOn);  
  16.   </PrepareResourcesDependsOn> 
  17. </PropertyGroup> 
  18. <Target 
  19.   Name="MergeDefaultStyles" 
  20.   Inputs="@(DefaultStyle)" 
  21.   Outputs="$(MSBuildProjectDirectory)\generic.xaml"> 
  22.   <MergeDefaultStylesTask 
  23.     DefaultStyles="@(DefaultStyle)" 
  24.     ProjectDirectory="$(MSBuildProjectDirectory)" /> 
  25. </Target> 
  26. <!--  
  27. Touch DefaultStyles on Rebuild to force generation of generic.xaml.  
  28. --> 
  29. <PropertyGroup> 
  30.   <RebuildDependsOn> 
  31.     TouchDefaultStyles;  
  32.     $(RebuildDependsOn);  
  33.   </RebuildDependsOn> 
  34. </PropertyGroup> 
  35. <Target Name="TouchDefaultStyles"> 
  36.   <Touch Files="@(DefaultStyle)" ForceTouch="true" /> 
  37. </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里面如下寫:

  1. <ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
  3.   <ResourceDictionary.MergedDictionaries> 
  4.     <ResourceDictionary Source="/Slippor.Controls;component/CustomControl/CustomControl.xaml"/> 
  5.   </ResourceDictionary.MergedDictionaries> 

</ResourceDictionary>這一方法適用于Silverlight 3及Silverlight 4。

原文作者:smjack

原文地址:http://www.cnblogs.com/smjack/archive/2010/08/24/1807706.html

【編輯推薦】

  1. 全屏模式下處理Silverlight控件的兩種方式
  2. 細數(shù)Silverlight 4的十二大引人注目新特性
  3. Silverlight 4中XAML解析的變化
責任編輯:王曉東 來源: 博客園
相關推薦

2020-03-03 13:53:10

AI金融IOT

2009-06-08 20:13:36

Eclipse自定義控

2013-04-19 10:14:24

2017-02-17 09:37:12

Android自定義控件方法總結

2018-09-30 15:08:41

2015-02-12 15:33:43

微信SDK

2015-02-12 15:38:26

微信SDK

2009-09-03 13:34:03

.NET自定義控件

2023-01-06 11:17:44

戴爾

2010-06-08 20:42:24

淘寶網(wǎng)釣魚欺詐

2014-09-24 11:42:46

AndroidButton

2021-11-18 13:40:50

物聯(lián)網(wǎng)人工智能IoT

2011-07-05 18:51:51

QT 控件 鼠標

2009-02-10 12:55:39

自定義控件AJAX.NET

2009-08-05 17:03:37

C#自定義控件

2009-08-06 17:13:56

ASP.NET自定義控

2021-08-16 14:45:38

鴻蒙HarmonyOS應用

2021-08-25 10:14:51

鴻蒙HarmonyOS應用

2015-02-11 17:49:35

Android源碼自定義控件

2009-08-03 13:34:06

自定義C#控件
點贊
收藏

51CTO技術棧公眾號