我只是在学习 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/