Используя MEF для импорта WPF DataTemplate?

Если все, что вы хотите сделать, это показать совпадения, вам нужно только:

ActiveDocument.Range.Find.HitHighlight FindText:="PQXY"
24
задан Scott Whitlock 9 May 2009 в 03:01
поделиться

1 ответ

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.

39
ответ дан 28 November 2019 в 23:42
поделиться
Другие вопросы по тегам:

Похожие вопросы: