How to provide XAML resources from MEF components

不打扰是莪最后的温柔 提交于 2019-11-30 05:27:28

问题


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.



来源:https://stackoverflow.com/questions/10530670/how-to-provide-xaml-resources-from-mef-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!