简体   繁体   中英

CompositionInitializer missing from MEF 4.5. What can I use instead?

I can`t find System.ComponentModel.Composition.Initialization.dll in .NET 4.5 (Which contains the declaration of the CompositionInitializer class). Has this assembly been removed from MEF in .NET 4.5? How can I now compose parts of an application marked with [Export] and [Import] attributes?

Suppose I have this view:

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

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

and the appropriate ViewModel:

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

What should I add in my shell (or elsewhere) to compose these parts?

The CompositionInitializer and similar classes exist in Silverlight, but not the full .NET Framework. The MEF team purposefully decided to leave them out, as they felt that they were not appropriate.

The logic was that construction injection should be used instead.

That being said, the logic which applied in Silverlight for using the class applies exactly the same in WPF. I have blogged about this in .NET 4 , and included an implementation that works for the full framework.

you lost init composition, fe

    _container = new CompositionContainer(catalog);

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

msdn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM