Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find resource dictionary file at runtime

Tags:

wpf

themes

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?

like image 666
Metro Avatar asked Dec 27 '22 20:12

Metro


2 Answers

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>
like image 140
Metro Avatar answered Jan 05 '23 01:01

Metro


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.

like image 23
yo chauhan Avatar answered Jan 05 '23 02:01

yo chauhan