PRISM + MEF-导入和ImportMany [英] PRISM + MEF -- Import & ImportMany

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

问题描述

FooService.cs :

public interface IFooService
{
    int Foo();
}

[Export("Foo1", typeof(IFooService))]
public class Foo1 : IFooService
{
    public int Foo() { return 1; }
}


[Export("Foo2", typeof(IFooService))]
public class Foo2 : IFooService
{
    public int Foo() { return 2; }
}

FooViewModel.cs :

public class FooViewModel : NotificationObject
{
    [ImportMany(typeof(IFooService))]
    public IEnumerable<IFooService> FooServices { get; private set; }

    [Import("Foo1")]
    public IFooService FirstFoo { get; private set; }
}


单次导入之所以有效,是因为我有一个已命名的合同,但是多次导入却没有.如果更改导出"属性并删除命名的合同,则多次导入有效,但单次导入无效.我如何才能同时工作?


The single import works because I have a named contract, however the multi import doesn't. If I change the Export attributes and remove the named contract, the multi import works, but the single import doesn't. How can I get both to work at the same time?

推荐答案

您可以在类中放置多个导出属性:

You can put multiple export attributes on your classes:

[Export(typeof(IFooService))]
[Export("Foo1", typeof(IFooService))]
public class Foo1 : IFooService
{
    public int Foo() { return 1; }
}

[Export(typeof(IFooService))]
[Export("Foo2", typeof(IFooService))]
public class Foo2 : IFooService
{
    public int Foo() { return 2; }
}

这篇关于PRISM + MEF-导入和ImportMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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