MEF 4.5中缺少CompositionInitializer。我可以用什么呢?

时间:2013-01-31 19:19:59

标签: c# .net mvvm dependency-injection mef

我无法在.NET 4.5中找到System.ComponentModel.Composition.Initialization.dll(其中包含CompositionInitializer类的声明)。是否已从.NET 4.5中的MEF中删除此程序集? 我现在如何组成标有[导出]和[导入]属性的应用程序的部分?

假设我有这个观点:

    internal partial class StartWindow : Window
    {
        public StartWindow()
        {
            InitializeComponent();
            DataContext = ViewModel;
        }

        [Import]
        public IStartWindowViewModel ViewModel { get; set; }
    }

和适当的ViewModel:

    [Export]
    internal class StartWindowViewModel : IStartWindowViewModel
    {
        public IEnumerable<Customer> Customers { get; set; }
    }

我应该在我的shell(或其他地方)添加什么来组成这些部分?

2 个答案:

答案 0 :(得分:5)

Silverlight中存在CompositionInitializer和类似的类,但不是完整的.NET Framework。 MEF团队故意决定将他们排除在外,因为他们觉得这些不合适。

逻辑是应该使用构造注入。

话虽这么说,Silverlight中使用该类的逻辑在WPF中应用完全相同。我有blogged about this in .NET 4,并包含一个适用于完整框架的实现。

答案 1 :(得分:1)

你失去了初始成分,f.e。

    _container = new CompositionContainer(catalog);

    //Fill the imports of this object
    try
    {
        this._container.ComposeParts(this);
    }
    catch (CompositionException compositionException)
    {
        Console.WriteLine(compositionException.ToString());
    }

msdn