简体   繁体   English

MEF进出口无效

[英]MEF import and export not working

I am trying to do a WPF application without using prism.When I tried to get my viewmodel in the view through mef by impoting constructor it not working.Also I have a property injection in my code that is also giving a null reference. 我试图在不使用棱镜的情况下创建WPF应用程序。当我试图通过强制构造函数来通过mef在视图中获取视图模型时,我的代码中也有一个属性注入,该注入也提供了空引用。 Is this because of not overriding composite container, if so how/where to give a composite container in an application without prism. 这是因为没有覆盖复合容器,如果这样,在没有棱镜的应用中如何/在何处给出复合容器。 My code is like this In xaml file design instance is set to viewmodel and cs file is like 我的代码是这样的在xaml文件设计实例中,设置为viewmodel,而cs文件是

MainWindow.xaml.cs MainWindow.xaml.cs

public MainWindow()
    {
        InitializeComponent();            
    }

    [ImportingConstructor]
    public MainWindow(MainWindowViewModel viewModel) :this()
    {
        this.DataContext = viewModel;
        this.ViewModel = viewModel;
    }
    public MainWindowViewModel ViewModel { get; set; }

[Export] 
public class MainWindowViewModel 
{    
}

the breakpoint at the constructor of MainWindow.xaml.cs is not hitting at all.Inside viewmodel the property injection is also not hitting. MainWindow.xaml.cs的构造函数的断点根本没有命中。在viewmodel内部,属性注入也没有命中。 How can I resolve this problem? 我该如何解决这个问题?

Here's an example using PRISM 5 but it'll work the same way in PRISM 6.2.0. 这是使用PRISM 5的示例,但在PRISM 6.2.0中将以相同的方式工作。

You'll need to remove the startup uri from app.xaml. 您需要从app.xaml中删除启动uri。 Then make sure you have the PRISM.MEF NuGet package installed. 然后确保已安装PRISM.MEF NuGet软件包。 Write a bootstrapper like the one below. 像下面这样写一个引导程序。 (Note that the bootstrapper base class has a range of methods to override with a proper place for all this setup. I have stuck it all in one method for simplicity.) (请注意,bootstrapper基类具有一系列方法,可以在所有此设置中使用适当的位置进行覆盖。为简单起见,我将所有方法都卡在一个方法中。)

public class MyBootstrapper : MefBootstrapper
{
    protected override DependencyObject CreateShell()
    {            
        var catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new AssemblyCatalog(typeof(MyBootstrapper).Assembly));
        var container = new CompositionContainer(catalog);
        var shell = container.GetExport<MainWindow>().Value;
        shell.Show();
        return shell;
    }
}

Add an Export attribute to your MainWindow. 在您的MainWindow中添加一个Export属性。 That will be needed for this line in the bootstrapper: 这对于引导程序中的这一行将是必需的:

var shell = container.GetExport<MainWindow>().Value;

[Export]
public partial class MainWindow : Window{}

Now all that is left is to run the bootstrapper: 现在剩下的就是运行引导程序了:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        var bootstrapper = new MyBootstrapper();
        bootstrapper.Run();
    }
}

Update 更新

You can get a pretty good handle on MEF by doing the example described here: 通过执行下面描述的示例,可以很好地掌握MEF:

https://msdn.microsoft.com/en-us/library/dd460648(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/dd460648(v=vs.110).aspx

Update 更新

Here's a cleaner way of implementing the bootstrapper. 这是实现引导程序的一种更干净的方法。

    public class MyBootstrapper : MefBootstrapper
    {
        protected override AggregateCatalog CreateAggregateCatalog()
        {
            return new AggregateCatalog();
        }

        protected override void ConfigureAggregateCatalog()
        {
            AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(MyBootstrapper).Assembly));
        }      

        protected override CompositionContainer CreateContainer()
        {
            return new CompositionContainer(AggregateCatalog);
        }

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
        }

        protected override DependencyObject CreateShell()
        {
            var shell = Container.GetExport<MainWindow>().Value;
            shell.Show();
            return shell;
        } 
    }

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

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