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?

Metro
  • 1,121
  • 1
  • 13
  • 33
  • Note that as per http://stackoverflow.com/questions/17083370/why-does-modifying-project-output-directories-cause-ioexception-was-unhandled output path also is an issue. – nietras Nov 11 '13 at 16:53

2 Answers2

9

I ended up having to use the syntax that includes the assembly name... don't ask me why

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/assemblyname;component/Themes/Classic.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary> 
</UserControl.Resources>
Lunyx
  • 3,164
  • 6
  • 30
  • 46
Metro
  • 1,121
  • 1
  • 13
  • 33
1

Hi try it without slash , we use slash when Build Action is Content or other but for ResourceDictionary build Action is Page.

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

I hope this will help. You must have some other names for these Resource Dictionaries because in Themes folder of the same project Generic.xaml is used as default and its resource can be accessed without mergeing it.

yo chauhan
  • 12,079
  • 4
  • 39
  • 58