Search code examples
c#wpfxamlresourcedictionaryxamlreader

Load dynamically a ResourceDictionnary


I'm having some issues. I want my app to load a ResourceDictonnary dynamically in app.xaml.cs.

This is my code for the moment :

ResourceDictionary theme = XamlReader.Load(???);
Resources.MergedDictionaries.Add(theme);

The problem is, how can I get my ResourceDictionnary stream from the xaml file ? I don't want to copy the xaml file with the exe. It's build action is set to Page and I want to load it.

Can you tell me how to do that ?

Thanks !


Solution

  • try using the following code (I call it from my IModule.Initialize)

     Application.Current.Resources.MergedDictionaries
                .Add(new ResourceDictionary
                {
                    Source = new Uri(@"pack://application:,,,/My.Application;component/Resources/Resources.xaml")
                });
    

    see the project tree in attached image: project tree in VS