wpf - wpf 上的 Caliburn.Micro 和 MEF

标签 wpf mef caliburn.micro

我只是在学习 WPF 和 Caliburn.Micro。我正在关注此处提供的代码:
http://caliburnmicro.codeplex.com/wikipage?title=Customizing%20The%20Bootstrapper&referringTitle=Documentation

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

文档说我需要直接在 wpf 中初始化容器,但是没有文档说明如何。如何直接初始化容器?

编辑 1
Bootstrap 在文档中是这样的:

     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 的初始化不正确。你能帮我修一下吗?

最佳答案

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

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

编辑

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

通常这将是您的 ShellViewModel声明看起来像这样:
[Export(typeof(IShell))]
public class ShellViewModel : PropertyChangedBase, IShell
{
   ...
}

您可以阅读有关 Bootstrap 设置和自定义的更多信息 here .

关于wpf - wpf 上的 Caliburn.Micro 和 MEF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12512535/

相关文章:

c# - WPF Caliburn MEF 应用程序和 DI

wpf - 如何立即更新 WPF 绑定(bind)

c# - WPF 绑定(bind)源

visual-studio-2010 - 在 Visual Studio 2010 中使用自定义 UI 编辑器注册自定义文件类型

c# - 实例化导出器期间出现异常

c# - Caliburn Micro ShowDialog 模态仅供查看

c# - 由于对象的当前状态,操作无效。当我删除按钮时

c# - InitializeComponent() 有什么作用,它在 WPF 中是如何工作的?

c# - 在 Windows 8 中获取类型程序集