简体   繁体   English

MEF和WPF无棱镜

[英]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. 有人可以告诉我一个在app.xaml.cs文件中构建MEF合成容器的示例,而无需使用棱镜或控制台应用程序,这没有问题。

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. 导出有效,但导入无效,我看到的所有示例仅适用于我不想使用的Prism。 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. 如果在App.xaml.cs文件中,导入将起作用,但是我不明白为什么在MainWindow.cs中导入将不起作用,并且一切都在根程序集中。

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. 如果我在MainWindow构造函数中进行合成,则可以编写它,但是如果可能的话,我想在app.xaml.cs中编写。

Here is a sample (I'm actually using mvvm but this example behaves the same with code behind). 这是一个示例(我实际上正在使用mvvm,但此示例的行为与后面的代码相同)。

 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());

    }
}
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; [Export(typeof(Window))]公共部分类MainWindow:Window {[Import]公共MessagePlugin插件{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");
        }


    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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