How to provide XAML resources from MEF components

2020-03-03 08:55发布

问题:

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. DataTemplates). At the moment this is implemented via DataTemplateSelectors 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

回答1:

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);
        }


回答2:

If your imported component will be contained only in the specific region of your application, then you could add only DataTemplate for the component's root ViewModel to the Resources of a FrameworkElement that represents a region where component will be located. All other DataTemplates (for other ViewModels in imported component) would then be contained in the Resources of the DataTemplate for component's root ViewModel.