改进NikhilKothari的换肤方案.doc_第1页
改进NikhilKothari的换肤方案.doc_第2页
改进NikhilKothari的换肤方案.doc_第3页
改进NikhilKothari的换肤方案.doc_第4页
改进NikhilKothari的换肤方案.doc_第5页
已阅读5页,还剩24页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

在 Silverlight 下换肤的实现中,我介绍了 Nikhil Kothari 实现的一种 Theme 方案。但是实践了一下,我很快发现有个小小的缺陷。作者的皮肤定义是仅针对系统自带的控件的,如 Button, T extBox, CheckBox 等,而对于我们自定义的控件的换肤问题没有很好的解决。从下列代码中可见一斑:(/Framework/Applications/Theme.cs)publicstringGetXml()stringresourceDictionaryFormat=0;_content.Replace(xmlns=/winfx/2006/xaml/presentation,);_content.Replace(xmlns:x=/winfx/2006/xaml,);_content.Replace(xmlns:vsm=clr-namespace:System.Windows;assembly=System.Windows,);returnString.Format(resourceDictionaryFormat,_content.ToString();可以看出这里名称空间都是写死了的,不支持任何扩展。而实际使用中,我们可能会对某个第三方控件定义样式。比如:这里定义了 Liquid 这套组件中 Dialog 控件的样式,而其中 xmlns:liquid 这个名称空间的声明就是必须的。要达到这个目标,我们可以修改 Theme.cs 的 parse 步骤,在其过程中补充一段收集 xml namespace 的代码,并在合并最终的样式 xaml 时加进去即可。修改过的 Theme.cs 代码如下:修改过的Theme.cs/Theme.cs/Copyright(c)NikhilKothari,2008.AllRightsReserved.//ThisproductscopyrightsarelicensedundertheCreative/CommonsAttribution-ShareAlike(version2.5).B//licenses/by-sa/2.5/Youarefreeto:/-usethisframeworkaspartofyourapp/-makeuseoftheframeworkinacommercialapp/aslongasyourapporproductisitselfnotaframework,controlpackor/developertoolkitofanysortunderthefollowingconditions:/Attribution.Youmustattributetheoriginalworkinyour/productorrelease./ShareAlike.Ifyoualter,transform,orbuildas-isuponthiswork,/youmayonlydistributetheresultingappsourceunder/alicenseidenticaltothisone./usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Text;usingSystem.Windows;usingSystem.Windows.Browser;usingSystem.Windows.Controls;usingSystem.Windows.Markup;usingSystem.Windows.Resources;usingSystem.Xml;namespaceSilverlight.FX.Applications/Representsathemecontrol,whosetop-levelresourceitemsaretobeincluded/whenthisthemeisselected./publicclassTheme:UserControlprivatestring_includes;/Anyadditionalthemestoinclude.Eachoftheadditionalthemesmustcontain/aUserControldeclaration.Thelistofthemesareprocessedinorderafter/thecurrentthemesresourceitemshavebeenincluded./publicstringIncludesgetreturn_includes?String.Empty;set_includes=value;privatestaticvoidExtractThemeContent(stringxml,ThemeInfotheme,boolextractIncludes)XmlReaderxmlReader=XmlReader.Create(newStringReader(xml),newXmlReaderSettingsCheckCharacters=false,DtdProcessing=DtdProcessing.Ignore,IgnoreComments=true,IgnoreProcessingInstructions=true,IgnoreWhitespace=true,);while(xmlReader.Read()if(xmlReader.NodeType=XmlNodeType.Element)/Foundthefirstelement/保存元素节点名称,因为下面要跳到属性去读取varelementName=xmlReader.Name;if(extractIncludes)theme.Includes=xmlReader.GetAttribute(Includes);/添加名称空间varhasAttr=xmlReader.MoveToFirstAttribute();if(hasAttr)doif(xmlReader.Name.StartsWith(xmlns:)varval=xmlReader.ReadContentAsString();theme.AddNamespace(xmlReader.Name,val);while(xmlReader.MoveToNextAttribute();stringresourcesTag=elementName+.Resources;/改为由父节点属性读到指定名称的子节点.不能用ReadToDescendant.if(xmlReader.ReadToFollowing(resourcesTag)/if(xmlReader.ReadToDescendant(resourcesTag)xmlReader.MoveToContent();/TODO:IwouldhaveexpectedMoveToContenttomovetothefirstitem/withinthecurrenttag,butapparentlythereaderisstill/positionedatthecurrenttag,unlessaReadisperformed.xmlReader.Read();while(xmlReader.EOF=false)if(xmlReader.NodeType=XmlNodeType.Element)stringkey=xmlReader.GetAttribute(x:Key);if(String.IsNullOrEmpty(key)=false)&(theme.ContainsKey(key)=false)stringmarkup=xmlReader.ReadOuterXml();theme.AddItem(key,markup);elsexmlReader.Skip();else/Nomoreitemsbreak;/Weonlylookattheresourcesoftheroottag,andsoweredonebreak;privatestaticstringGetThemeXaml(stringthemeName,stringfileName,boollookupShared)StreamResourceInforesourceInfo=null;/Firsttry/Themes/.xamlUriresourceUri=newUri(Themes/+themeName+/+fileName+.xaml,UriKind.Relative);resourceInfo=Application.GetResourceStream(resourceUri);if(resourceInfo=null)&lookupShared)/Thefallbackisonelevelup,i.e./Themes/.xamlresourceUri=newUri(Themes/+fileName+.xaml,UriKind.Relative);resourceInfo=Application.GetResourceStream(resourceUri);if(resourceInfo=null)thrownewInvalidOperationException(Thefilenamed+fileName+inthethemenamed+themeName+couldnotbefound.);StreamReaderresourceReader=newStreamReader(resourceInfo.Stream);returnresourceReader.ReadToEnd();internalstaticvoidLoadTheme(ResourceDictionarytargetResources,stringname)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);ResourceDictionarythemeResources=userControl.Resources;foreach(stringkeyintheme.Keys)targetResources.Add(key,themeResourceskey);return;catch(Exceptione)thrownewInvalidOperationException(Thethemenamed+name+containedinvalidXAML.,e);if(theme.Includes.Length!=0)stringincludes=theme.Includes.Split(newchar,StringSplitOptions.RemoveEmptyEntries);for(inti=0;iincludes.Length;i+)stringincludeXaml=GetThemeXaml(name,includesi,/*lookupShared*/true);tryExtractThemeContent(includeXaml,theme,/*extractIncludes*/false);catch(Exceptione)thrownewInvalidOperationException(Theincludenamed+includesi+inthethemenamed+name+containedinvalidXAML.,e);trystringmergedXaml=theme.GetXml();UserControluserControl=(UserControl)XamlReader.Load(mergedXaml);ResourceDictionarythemeResources=userControl.Resources;foreach(stringkeyintheme.Keys)targetResources.Add(key,themeResourceskey);catch(Exceptione)thrownewInvalidOperationException(Thethemenamed+name+containedinvalidXAML.,e);privatesealedclassThemeInfoprivateStringBuilder_content;privateList_keys;privateDictionary_keyMap;privatestring_includes;publicThemeInfo()_content=newStringBuilder();_keys=newList();_keyMap=newDictionary();InitXmlNamespaces();publicstringIncludesgetreturn_includes?String.Empty;set_includes=value;publicICollectionKeysgetreturn_keys;publicvoidAddItem(stringkey,stringxml)_keyMapkey=String.Empty;_keys.Add(key);_content.Append(xml);publicboolContainsKey(stringkey)return_keyMap.ContainsKey(key);/增加自定义名称空间的功能privateDictionary_xmlNamespaces;/初始化名称空间字典privatevoidInitXmlNamespaces()_xmlNamespaces=newDictionary();_xmlNamespacesxmlns=/winfx/2006/xaml/presentation;_xmlNamespacesxmlns:x=/winfx/2006/xaml;_xmlNamespacesxmlns:vs

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论