Search code examples
wpfmef

MEF and WPF without Prism


Can someone please show me an example of building the MEF Composition Container in the app.xaml.cs file without using prism or a console app which is no problems.

The Exports work but the imports don't and all the examples I see are only working with Prism which I don't want to use. The import will work if in the App.xaml.cs file but I don't understand why the Import won't work in the MainWindow.cs and everything is in the root assembly.

I can get it to compose if I do the composition in the MainWindow constructor but I would like to compose in app.xaml.cs if possible.

Here is a sample (I'm actually using mvvm but this example behaves the same with code behind).

 public partial class App : Application
{
    public App()
    {
        ShutdownMode = ShutdownMode = ShutdownMode.OnMainWindowClose;

    }

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);



        Compose();

       var window = new MainWindow();
        window.Show();

    }

    public void Compose()
    {
        var catalog = new AggregateCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()), new DirectoryCatalog("."));
        var container = new CompositionContainer(catalog);
        container.ComposeParts(this);
    }


}

 [Export]
public class MessagePlugin
{
    public string GetMessage()
    {
        return "Successfully composed message";
    }

}

 public partial class MainWindow : Window
{
    [Import]
    public MessagePlugin plugin { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        Loaded += MainWindow_Loaded;

    }


    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var p = plugin; <-------------------------------NULL
        MessageBox.Show(p.GetMessage());

    }
}

Solution

  • public partial class App : Application
    {
       private CompositionContainer container;
    
        [Import(typeof(Window))]
        public Window TheMainWindow { get; set; }
    
    
        public App()
        {
            ShutdownMode = ShutdownMode = ShutdownMode.OnMainWindowClose;
        }
    
    
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
    
            TheMainWindow = new MainWindow();
    
            Compose();
    
            Application.Current.MainWindow = TheMainWindow;
            Application.Current.MainWindow.Show();
    
        }
    
        public void Compose()
        {
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(App).Assembly));  
            container = new CompositionContainer(catalog);
            container.ComposeParts(this);
        }
    
    }
    

    [Export(typeof(Window))] public partial class MainWindow : Window { [Import] public MessagePlugin plugin { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
    
        }
    
    
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var p = plugin;
    
            if (p != null)
            {
                MessageBox.Show(p.GetMessage());
            }
            else
            {
                MessageBox.Show("Plugin NOT Composed");
            }
    
    
        }
    }