How to provide XAML resources from MEF components

I have an imported MEF component that loads dynamically when I open the import wizard. As soon as the user selects the type of import that she wants to process, the control of the dialog box of the import wizard is transferred to the selected import component. Of course, import components must supply resources to the wizard's dialog box (for example, DataTemplates). This is currently implemented through DataTemplateSelectorwhich are provided by import components. They access the local ResourceDictionaryassembly of the import component.

But, as you can imagine, this is tedious: I have to add code for each DataTemplateto provide, WPF does not automatically use the correct one DataTemplatefor the type ViewModelthat is displayed.

Has anyone solved this problem before? How do you guys provide resources in a plugin environment?

Thanks for any help in advance.

Regards

+5
source share
2 answers

I lost information about where I found this little trick, but one thing you can do is dynamically import resource dictionaries when building external assemblies.

In each assembly with resources, you export one or more ResourceDictionary objects, sorting through the code and commenting this as follows:

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

, [ImportMany] IEnumerable<ResourceDictionary> resourceDictionaries - :

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

, DataTemplate ViewModel FrameworkElement, , . DataTemplates ( ViewModels ) DataTemplate ViewModel .

0

All Articles