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:
NonShared
behaviour, so I can create multiple shells using MEF/ServiceLocator.Cheers,
Mark
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).