改進NikhilKothari的換膚方案_第1頁
改進NikhilKothari的換膚方案_第2頁
改進NikhilKothari的換膚方案_第3頁
改進NikhilKothari的換膚方案_第4頁
改進NikhilKothari的換膚方案_第5頁
已閱讀5頁,還剩26頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、在 Silverlight 下換膚的實現中,我介紹了 Nikhil Kothari 實現的一種 Theme 方案。但是實踐了一下,我很快發(fā)現有個小小的缺陷。作者的皮膚定義是僅針對系統自帶的控件的,如 Button, T extBox, CheckBox 等,而對于我們自定義的控件的換膚問題沒有很好的解決。從下列代碼中可見一斑:(/Framework/Applications/Theme.cs)publicstringGetXml()stringresourceDictionaryFormat="<UserControlxmlns=""xmlns:x=&quo

2、t;"xmlns:vsm=""clr-namespace:System.Windows;assembly=System.Windows""><UserControl.Resources>0</UserControl.Resources></UserControl>"_content.Replace("xmlns="""");_content.Replace("xmlns:x="""");_con

3、tent.Replace("xmlns:vsm=""clr-namespace:System.Windows;assembly=System.Windows""","");returnString.Format(resourceDictionaryFormat,_content.ToString();方案范文無法思考和涵蓋全面,最好仔細瀏覽后下載使用??梢钥闯鲞@里名稱空間都是寫死了的,不支持任何擴展。而實際使用中,我們可能會對某個第三方控件定義樣式。比如:方案范文無法思考和涵蓋全面,最好仔細瀏覽后下載使用。<

4、UserControlxmlns="xmlns:x="xmlns:d="xmlns:mc="/markup-compatibility/2006"xmlns:liquid="clr-namespace:Liquid;assembly=Liquid"mc:Ignorable="d"x:Class="UserControl"d:DesignWidth="640"d:DesignHeight="48

5、0"><UserControl.Resources><Stylex:Key="MyLiquidDialog"TargetType="liquid:Dialog"><!-略-></Style></UserControl.Resources><Gridx:Name="LayoutRoot"Background="White"/></UserControl>這里定義了 Liquid 這套組件中 Dialog 控件的樣式,而

6、其中 xmlns:liquid 這個名稱空間的聲明就是必須的。要達到這個目標,我們可以修改 Theme.cs 的 parse 步驟,在其過程中補充一段收集 xml namespace 的代碼,并在合并最終的樣式 xaml 時加進去即可。修改過的 Theme.cs 代碼如下:方案范文無法思考和涵蓋全面,最好仔細瀏覽后下載使用。修改過的Theme.cs/Theme.cs/Copyright(c)NikhilKothari,2008.AllRightsReserved./Thisproduct'scopyrightsarelicensedundertheCreative/CommonsAtt

7、ribution-ShareAlike(version2.5).B//licenses/by-sa/2.5/Youarefreeto:/-usethisframeworkaspartofyourapp/-makeuseoftheframeworkinacommercialapp/aslongasyourapporproductisitselfnotaframework,controlpackor/developertoolkitofanysortunderthefollowingconditions:/Attribution.Youmustat

8、tributetheoriginalworkinyour/productorrelease./ShareAlike.Ifyoualter,transform,orbuildas-isuponthiswork,/youmayonlydistributetheresultingappsourceunder/alicenseidenticaltothisone./usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Text;usingSystem.Windows;usingSystem.Windows.Brow

9、ser;usingSystem.Windows.Controls;usingSystem.Windows.Markup;usingSystem.Windows.Resources;usingSystem.Xml;namespaceSilverlight.FX.Applications/<summary>/Representsathemecontrol,whosetop-levelresourceitemsaretobeincluded/whenthisthemeisselected./</summary>publicclassTheme:UserControlpriva

10、testring_includes;/<summary>/Anyadditionalthemestoinclude.Eachoftheadditionalthemesmustcontain/aUserControldeclaration.Thelistofthemesareprocessedinorderafter/thecurrenttheme'sresourceitemshavebeenincluded./</summary>publicstringIncludesgetreturn_includes?String.Empty;set_includes=va

11、lue;privatestaticvoidExtractThemeContent(stringxml,ThemeInfotheme,boolextractIncludes)XmlReaderxmlReader=XmlReader.Create(newStringReader(xml),newXmlReaderSettingsCheckCharacters=false,DtdProcessing=DtdProcessing.Ignore,IgnoreComments=true,IgnoreProcessingInstructions=true,IgnoreWhitespace=true,);wh

12、ile(xmlReader.Read()if(xmlReader.NodeType=XmlNodeType.Element)/Foundthefirstelement/保存元素節(jié)點名稱,因為下面要跳到屬性去讀取varelementName=xmlReader.Name;if(extractIncludes)theme.Includes=xmlReader.GetAttribute("Includes");/添加名稱空間varhasAttr=xmlReader.MoveToFirstAttribute();if(hasAttr)doif(xmlReader.Name.Star

13、tsWith("xmlns:")varval=xmlReader.ReadContentAsString();theme.AddNamespace(xmlReader.Name,val);while(xmlReader.MoveToNextAttribute();stringresourcesTag=elementName+".Resources"/改為由父節(jié)點屬性讀到指定名稱的子節(jié)點.不能用ReadToDescendant.if(xmlReader.ReadToFollowing(resourcesTag)/if(xmlReader.ReadToDes

14、cendant(resourcesTag)xmlReader.MoveToContent();/TODO:IwouldhaveexpectedMoveToContenttomovetothefirstitem/withinthecurrenttag,butapparentlythereaderisstill/positionedatthecurrenttag,unlessaReadisperformed.xmlReader.Read();while(xmlReader.EOF=false)if(xmlReader.NodeType=XmlNodeType.Element)stringkey=x

15、mlReader.GetAttribute("x:Key");if(String.IsNullOrEmpty(key)=false)&&(theme.ContainsKey(key)=false)stringmarkup=xmlReader.ReadOuterXml();theme.AddItem(key,markup);elsexmlReader.Skip();else/Nomoreitemsbreak;/Weonlylookattheresourcesoftheroottag,andsowe'redonebreak;privatestaticst

16、ringGetThemeXaml(stringthemeName,stringfileName,boollookupShared)StreamResourceInforesourceInfo=null;/Firsttry/Themes/<ThemeName>/<FileName>.xamlUriresourceUri=newUri("Themes/"+themeName+"/"+fileName+".xaml",UriKind.Relative);resourceInfo=Application.GetReso

17、urceStream(resourceUri);if(resourceInfo=null)&&lookupShared)/Thefallbackisonelevelup,i.e./Themes/<FileName>.xamlresourceUri=newUri("Themes/"+fileName+".xaml",UriKind.Relative);resourceInfo=Application.GetResourceStream(resourceUri);if(resourceInfo=null)thrownewInval

18、idOperationException("Thefilenamed'"+fileName+"inthethemenamed'"+themeName+"'couldnotbefound.");StreamReaderresourceReader=newStreamReader(resourceInfo.Stream);returnresourceReader.ReadToEnd();internalstaticvoidLoadTheme(ResourceDictionarytargetResources,str

19、ingname)ThemeInfotheme=newThemeInfo();stringthemeXaml=GetThemeXaml(name,"Theme",/*lookupShared*/false);tryExtractThemeContent(themeXaml,theme,/*extractIncludes*/true);if(theme.Includes.Length=0)&&(theme.Keys.Count!=0)UserControluserControl=(UserControl)XamlReader.Load(themeXaml);Re

20、sourceDictionarythemeResources=userControl.Resources;foreach(stringkeyintheme.Keys)targetResources.Add(key,themeResourceskey);return;catch(Exceptione)thrownewInvalidOperationException("Thethemenamed'"+name+"'containedinvalidXAML.",e);if(theme.Includes.Length!=0)stringincl

21、udes=theme.Includes.Split(newchar',','',StringSplitOptions.RemoveEmptyEntries);for(inti=0;i<includes.Length;i+)stringincludeXaml=GetThemeXaml(name,includesi,/*lookupShared*/true);tryExtractThemeContent(includeXaml,theme,/*extractIncludes*/false);catch(Exceptione)thrownewInvalidOpe

22、rationException("Theincludenamed'"+includesi+"'inthethemenamed'"+name+"'containedinvalidXAML.",e);trystringmergedXaml=theme.GetXml();UserControluserControl=(UserControl)XamlReader.Load(mergedXaml);ResourceDictionarythemeResources=userControl.Resources;fo

23、reach(stringkeyintheme.Keys)targetResources.Add(key,themeResourceskey);catch(Exceptione)thrownewInvalidOperationException("Thethemenamed'"+name+"'containedinvalidXAML.",e);privatesealedclassThemeInfoprivateStringBuilder_content;privateList<string>_keys;privateDictio

24、nary<string,string>_keyMap;privatestring_includes;publicThemeInfo()_content=newStringBuilder();_keys=newList<string>();_keyMap=newDictionary<string,string>();InitXmlNamespaces();publicstringIncludesgetreturn_includes?String.Empty;set_includes=value;publicICollection<string>Ke

25、ysgetreturn_keys;publicvoidAddItem(stringkey,stringxml)_keyMapkey=String.Empty;_keys.Add(key);_content.Append(xml);publicboolContainsKey(stringkey)return_keyMap.ContainsKey(key);/增加自定義名稱空間的功能privateDictionary<string,string>_xmlNamespaces;/初始化名稱空間字典privatevoidInitXmlNamespaces()_xmlNamespaces=newDictionary<string,string>();_xmlNamespaces"xmlns"="_xmlNamespaces"xmlns:x"="_xmlNamespaces"xmlns:vsm"="clr-namespace:System.Windows;assembly=System.Windows"/組合名稱空間屬性privatestringGetNamespaceAttributes()va

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論