使用 MEF 从 Prism 4 中的 CompositionContainer 获取新的 Shell 实例
我在 Prism 中遇到了一些奇怪的 MEF 行为,我无法真正解释。我找到了一种解决方法,但我不太满意,所以我真的很想了解造成这种情况的原因。
我已使用 NonShared
的 PartCreationPolicy
声明了我的 shell 窗口类。我尝试使用 MefBootstrapper
中的 CompositionContainer.GetExportedValue<>()
函数来创建 Shell 的新实例。
奇怪的是,如果我在创建 shell 之前调用 Container.GetExportedValue<>() ,每次调用时我都会得到一个 Shell
类型的新对象它。但是,一旦 Shell 初始化,重复调用 Container.GetExportedValue<>() 将返回 Shell 的相同实例。
就好像 shell 初始化以某种方式将我的 Shell 导出重新注册为共享。
但是,我在引导程序代码中没有看到任何明确尝试实现此目的的调用。
谁能解释一下:
- 什么操作有此副作用
- 如何(如果可能)恢复
NonShared
行为,以便我可以使用 MEF/ServiceLocator 创建多个 shell。
干杯,
马克
I'm coming across some strange MEF behaviour in Prism, which I can't really explain. I've found a way around it that I'm not too happy with, so I'd really like to understand what's causing it.
I've declared my shell window class with a PartCreationPolicy
of NonShared
. And I'm trying to use the CompositionContainer.GetExportedValue<>()
function from my MefBootstrapper
to create a new instance of the Shell.
The strange thing is, if I call Container.GetExportedValue<>()
before the shell has been created, I get a new object of type Shell
, each time I call it. However, once the shell has been initialized, repeated calls to Container.GetExportedValue<>()
return the same instance of the Shell.
It's as if the shell initialization somehow re-registers my Shell export as a Shared.
However, I don't see any calls in the bootstrapper code that explicitly try to achieve this.
Can anyone explain:
- what action has this side effect
- How (if possible) to restore the
NonShared
behaviour, so I can create multiple shells using MEF/ServiceLocator.
Cheers,
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定 Prism 如何使用 MEF,但这里有一个理论:正常启动时如何创建 shell?我的猜测是,这不是通过从 MEF 容器调用 GetExportedValue,而是通过调用 Shell 的构造函数,然后通过 ComposeParts() 或使用 CompositionBatch 将其添加到容器。以这种方式直接添加到容器的部件将覆盖目录中可用的部件,并且 CreationPolicy 也不会应用(因为 MEF 不会创建该部件)。
I'm not sure how Prism uses MEF, but here's a theory: How is the shell being created in normal startup? My guess is that it is not by calling GetExportedValue from the MEF container, but rather by calling the constructor for the Shell and then adding it to the container via ComposeParts() or using a CompositionBatch. A part added directly to the container in that way would override what was available in the catalog, and the CreationPolicy wouldn't apply either (because MEF isn't creating that part).
这里是一个答案对于您的多外壳问题。您必须检查 NonShared 行为是否在那里得到解答。
here is a answer for your multiple shell question. you have to check if the NonShared behavior is answered there.