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

查看:20
本文介绍了使用 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 的应用程序框架作为开源,并广泛使用这种方法将视图导入到应用程序资源中.效果很好,可以自己下载源码看看效果如何.

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天全站免登陆