4

I am developing a user control and use it inside an ElementHost. I define the resource dictionary as follows:

<UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Themes/Classic.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary> 
    </UserControl.Resources>

In my VS explorer, I have this

Project (the user control library)
  |_ Themes
       |__ Generic.xaml  (Build Action = Page)
       |__ Classic.xaml  (Build Action = Page)

There's no compilation error and the VS designer seems to pick up the resources defined in Classic.xaml

However, it crashes at runtime with the following exception:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Windows.Markup.XamlParseException: 'Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number '16' and line position '18'. ---> System.IO.IOException: Cannot locate resource 'themes/classic.xaml'.

What's going on?

4

2 回答 2

9

我最终不得不使用包含程序集名称的语法......不要问我为什么

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/assemblyname;component/Themes/Classic.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary> 
</UserControl.Resources>
于 2012-07-31T14:05:30.100 回答
1

嗨,试试不带斜线,当构建操作是内容或其他时,我们使用斜线,但对于 ResourceDictionary,构建操作是页面。

 <ResourceDictionary Source="Themes/Classic.xaml"/>
 <ResourceDictionary Source="Themes/Generic.xaml"/>

我希望这将有所帮助。您必须为这些资源字典设置一些其他名称,因为在同一项目的主题文件夹中 Generic.xaml 被用作默认值,并且无需合并即可访问其资源。

于 2012-07-27T16:59:37.660 回答