2010-06-11 36 views
7

我开发了一个允许用户在主题之间切换的应用程序。我被包括XAML文件在我的项目资源,并使用下面的代码这样做:在Silverlight中使用资源字典作为主题

MainTheme.ThemeUri = new Uri("SilverlightApplication1;component/Themes/[ThemeName]/Theme.xaml", UriKind.Relative); 

这个效果很好,直到我发现了以下主题:http://timheuer.com/blog/archive/2010/05/17/silverlight-4-tools-released-and-new-application-templates.aspx

不同的是,这些主题由多个文件组成。所以我制作了一个只包含MergedDictionaries的Theme.xaml文件,这样我仍然可以使用上面的代码。这是Cosmopolitan主题的Theme.xaml文件。

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="CoreStyles.xaml"/> 
     <ResourceDictionary Source="SDKStyles.xaml"/> 
     <ResourceDictionary Source="Styles.xaml"/> 
     <ResourceDictionary Source="ToolkitStyles.xaml"/> 
    </ResourceDictionary.MergedDictionaries> 
</ResourceDictionary> 

然而,当我运行的C#代码上面,我得到以下异常:

System.Windows.Markup.XamlParseException: Failed to assign to property 'System.Windows.ResourceDictionary.Source'. 

只是要清楚,使用MergedDictionaries方法不工作时,我把它在我的App.xaml:

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Themes/Cosmopolitan/Theme.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

我在做什么错?

谢谢!

+0

我Theme.xaml文件中设置绝对路径也不起作用:( – SaphuA 2010-06-13 11:36:57

+0

我有一个类似的问题原因是在引用文件时使用了反斜杠(\)而不是正斜杠(/).VS中的xaml解析器能够解析位置,但是在运行时会产生一个错误,希望这会帮助其他人out。 – 2011-08-09 18:26:47

回答

10

当您使用MergedDictionary时,您必须使用下面的完全限定名称。

<ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="/SilverlightApplication1;component/Themes/Cosmopolitan/Theme.xaml"/> 

此外,请注意,您不应该在程序集名称之前忽略斜线。换句话说,它应该像

Source="/SilverlightApplication1; 

不喜欢

Source="SilverlightApplication1; 

HTH

+5

你不必把'component'放在船尾呃程序集名称“/SilverlightApplication1;component/Themes/Cosmopolitan/Theme.xaml” – user20358 2010-12-12 09:01:57

+0

是的,你应该把组件。这是正确的。 – Lance 2011-09-15 04:12:09

+1

@王子:组成部分是必不可少的。请考虑更新您的答案。在更新之后这将是一个很好的答案。 – 2011-12-14 23:28:42