Caliburn.Micro和MEF在wpf上

man*_*ans 13 wpf mef caliburn.micro

我刚刚学习WPF和Caliburn.Micro.我正在按照此处提供的代码:http: //caliburnmicro.codeplex.com/wikipage?title = Customizing20The%20Bootstrapper&referringTitle=Documentation

显然这段代码适用于Silverlight,但我的项目是WPF,因此我收到的错误是CompositionHost未定义.

该文件表明我需要直接在wpf中初始化容器,但是没有文档可以显示如何.如何直接初始化容器?

编辑1引导过滤器在文档中是这样的:

     container = CompositionHost.Initialize(
        new AggregateCatalog(
            AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
            )
        );

    var batch = new CompositionBatch();

    batch.AddExportedValue<IWindowManager>(new WindowManager());
    batch.AddExportedValue<IEventAggregator>(new EventAggregator());
    batch.AddExportedValue(container);

    container.Compose(batch);
Run Code Online (Sandbox Code Playgroud)

我把它转换成:

    var catalog =
            new AggregateCatalog(
                AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>());

        this.container = new CompositionContainer(catalog);
        var batch = new CompositionBatch();

        batch.AddExportedValue<IWindowManager>(new WindowManager());
        batch.AddExportedValue<IEventAggregator>(new EventAggregator());
        batch.AddExportedValue(this.container);

        this.container.Compose(batch);
Run Code Online (Sandbox Code Playgroud)

但是当我运行应用程序时,我收到的错误是MEF无法找到IShell的实现

     Could not locate any instances of contract IShell.
Run Code Online (Sandbox Code Playgroud)

我相信我的MEF初始化是不正确的.你能帮我解决一下吗?

And*_*son 17

在WPF中,您需要使用显式CompositionContainer构造函数.在我的WPF和Silverlight共享引导程序我已经使用了以下#if- #else指令:

#if SILVERLIGHT
    container = CompositionHost.Initialize(catalog);
#else
    container = new CompositionContainer(catalog); ;
#endif
Run Code Online (Sandbox Code Playgroud)

编辑

引导程序将识别实现IShell接口的组件(假设您的引导程序正在扩展Bootstrapper<IShell>基类),因此您需要实现一个用MEF导出的类来装饰IShell.

通常这将是你的ShellViewModel,声明将如下所示:

[Export(typeof(IShell))]
public class ShellViewModel : PropertyChangedBase, IShell
{
   ...
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处阅读有关引导程序设置和自定义的更多信息.