How to provide XAML resources from MEF components

mef, resourcedictionary, resources, wpf

Solution

I lost track of where I found this little trick, but one thing you can do is dynamically import resource dictionaries when you compose external assemblies.

In each assembly with resources, you export one or more ResourceDictionary objects by going code behind and annotating like this:

[Export(typeof(ResourceDictionary))]
public partial class Resources : ResourceDictionary
{
    public Resources()
    {
        InitializeComponent();
    }
}

Now you need a component that resolves an `[ImportMany] IEnumerable<ResourceDictionary> resourceDictionaries` and do something like this:

        //Merge exported resource dictionaries from all composed sources into the application
        foreach (var resourceDictionary in resourceDictionaries)
        {
            Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
        }

Problem

I have an import MEF component which is dynamically loaded when the import wizard opens. As soon as the user selects the type of import she wants to process, the control over the import wizard dialog is passed to the chosen import component. Of course, import components need to supply resources to the wizard dialog (e. g. `DataTemplate`s). At the moment this is implemented via `DataTemplateSelector`s which are provided by the import components. They access a local `ResourceDictionary` of the import component's assembly. But as you can imagine, this is tedious: I have to add code for every `DataTemplate` to provide, WPF does not automatically use the right `DataTemplate` by the type of the `ViewModel` being displayed. Has anybody solved this problem before? How do you guys out there provide resources in a plug-in environment? Thank you for any help in advance. Best regards

Original source