Using MEF to import a WPF DataTemplate?

后端 未结 1 890
囚心锁ツ
囚心锁ツ 2020-12-02 15:10

I was looking at MEF as an extensibility framework, and I\'m pretty much sold, except for one point:

Let\'s say I want to import both a ViewModel and a View to displ

相关标签:
1条回答
  • 2020-12-02 15:16

    Yes, I was able to make this work in the following way:

    In my host WPF application, I added this Import:

        [ImportMany("ApplicationResources", typeof(ResourceDictionary))]
        public IEnumerable<ResourceDictionary> Views { get; set; }
    

    Then in my composite part, I declared a ViewModel, and a data template for the ViewModel in a regular ResourceDictionary Xaml file. Then I created a code behind for the ResourceDictionary, like this (in this example, the ViewModel is called ItemViewModel and the ResourceDictionary is called ItemView):

    [Export("ApplicationResources", typeof(ResourceDictionary))]
    public partial class ItemView : ResourceDictionary 
    {
        public ItemView()
        {
            InitializeComponent();
        }
    }
    

    For reference, the Xaml for the example ResourceDictionary looks like this:

    <ResourceDictionary 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyCompany.MyProduct"
        x:Class="MyCompany.MyProduct.ItemView">
    
        <DataTemplate DataType="{x:Type local:ItemViewModel}">
            ...
        </DataTemplate>
    
    </ResourceDictionary>
    

    Then, back in my host WPF application, after I successfully compose and before I show the main window, I do this:

    // Add the imported resource dictionaries
    // to the application resources
    foreach (ResourceDictionary r in Views)
    {
        this.Resources.MergedDictionaries.Add(r);
    }
    

    That seems to successfully apply the DataTemplate anywhere WPF sees an ItemViewModel.

    EDIT: For anyone who's interested, I released an application framework called SoapBox Core as open source, and it uses this method extensively to import Views into the application resources. It works very well, and you can download the source yourself and take a look at how it works.

    0 讨论(0)
提交回复
热议问题