13

我只是在学习 WPF 和 Caliburn.Micro。我正在关注此处提供的代码: http ://caliburnmicro.codeplex.com/wikipage?title=Customizing%20The%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);

我将其转换为:

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

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

     Could not locate any instances of contract IShell.

我相信我对 MEF 的初始化不正确。你能帮我修一下吗?

4

1 回答 1

18

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

#if SILVERLIGHT
    container = CompositionHost.Initialize(catalog);
#else
    container = new CompositionContainer(catalog); ;
#endif

编辑

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

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

[Export(typeof(IShell))]
public class ShellViewModel : PropertyChangedBase, IShell
{
   ...
}

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

于 2012-09-20T15:16:02.967 回答