WPF 上的 Caliburn.Micro 和 MEF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12512535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Caliburn.Micro and MEF on wpf
提问by mans
I am just learning WPF and Caliburn.Micro. I am following the code that presented here: http://caliburnmicro.codeplex.com/wikipage?title=Customizing%20The%20Bootstrapper&referringTitle=Documentation
我只是在学习 WPF 和 Caliburn.Micro。我正在关注此处提供的代码:http: //caliburnmicro.codeplex.com/wikipage?title=Customizing%20The%20Bootstrapper&referringTitle=Documentation
Apparently this code is for Silverlight but my project is a WPF and for this reason I am getting error that CompositionHost is not defined.
显然,此代码适用于 Silverlight,但我的项目是 WPF,因此我收到未定义 CompositionHost 的错误。
The document stated that I need to initialize the container directly in wpf, but there is no document to show how. How can I initialize the container directly?
文档说我需要直接在wpf中初始化容器,但是没有文档说明怎么做。如何直接初始化容器?
Edit 1 The boot strapper is like this in documentation:
编辑 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);
and I converted it to :
我将其转换为:
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);
But when I run application, I am getting error that MEF can not find implementation for IShell
但是当我运行应用程序时,我收到错误消息,MEF 找不到 IShell 的实现
Could not locate any instances of contract IShell.
I belive my initialization of MEF is not correct. Can you please help me fix it?
我相信我对 MEF 的初始化不正确。你能帮我修一下吗?
回答by Anders Gustafsson
In WPF you need to use an explicit CompositionContainerconstructor. In my WPF and Silverlight shared bootstrapper I have used the following #if-#elsedirective:
在 WPF 中,您需要使用显式CompositionContainer构造函数。在我的 WPF 和 Silverlight 共享引导程序中,我使用了以下#if-#else指令:
#if SILVERLIGHT
container = CompositionHost.Initialize(catalog);
#else
container = new CompositionContainer(catalog); ;
#endif
EDIT
编辑
The bootstrapper will identify a component that implements the IShellinterface (provided your bootstrapper is extending the Bootstrapper<IShell>base class), so you need to implement a class decorated with MEF export of IShell.
引导程序将识别实现该IShell接口的组件(前提是您的引导程序正在扩展Bootstrapper<IShell>基类),因此您需要实现一个用 MEF 导出的IShell.
Typically this would be your ShellViewModeland the declaration would look like this:
通常,这将是您的ShellViewModel,并且声明将如下所示:
[Export(typeof(IShell))]
public class ShellViewModel : PropertyChangedBase, IShell
{
...
}
You can read much more about bootstrapper setup and customization here.
您可以在此处阅读有关引导程序设置和自定义的更多信息。

