使用MEF导入WPF DataTemplate? [英] Using MEF to import a WPF DataTemplate?

查看:65
本文介绍了使用MEF导入WPF DataTemplate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直把MEF看作是一种可扩展性框架,除了一点以外,我已经卖了很多:

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

假设我要导入一个ViewModel和一个View来显示它.我认为正确"的方法是让MEF部分导出ViewModel类和显示ViewModel的DataTemplate.例如,假设您正在构建类似Visio的应用程序,并且想要导入形状库.每个形状都需要一个在Xaml中定义的View和一个用于包装一些基础Model对象的ViewModel.

Let's say I want to import both a ViewModel and a View to display it. I think the "right" way to do that is for the MEF part to export a ViewModel class, and a DataTemplate that displays the ViewModel. As an example, say you were building a Visio-like application and you want to import a library of shapes. Each shape needs a View defined in Xaml and a ViewModel that would wrap some underlying Model object.

这可能吗?对于DataTemplate,导入合同将是什么样子?如何使WPF知道导入的DataTemplate?

Is this possible? What would the Import contract look like for the DataTemplate and how do I make WPF aware of the imported DataTemplate?

推荐答案

是的,我能够通过以下方式进行这项工作:

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

在主机WPF应用程序中,我添加了此导入:

In my host WPF application, I added this Import:

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

然后在复合部分中,我在常规ResourceDictionary Xaml文件中声明了一个ViewModel和一个用于ViewModel的数据模板.然后,我为ResourceDictionary创建了一个代码,如下所示(在本示例中,ViewModel称为ItemViewModel,而ResourceDictionary称为ItemView):

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

作为参考,示例ResourceDictionary的Xaml如下所示:

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>

然后,在我成功完成撰写之后并且在显示主窗口之前,回到我的主机WPF应用程序中:

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

这似乎可以在WPF看到ItemViewModel的任何地方成功应用DataTemplate.

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

编辑:对于感兴趣的人,我发布了一个名为 SoapBox Core 的应用程序框架,开源,并且广泛使用此方法将View导入到应用程序资源中.效果很好,您可以自己下载源代码并查看其工作方式.

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.

这篇关于使用MEF导入WPF DataTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆