Jan 16, 2012 at 8:26 PM
Edited Jan 16, 2012 at 8:28 PM
I have a Prism/MEF/MVVM solution with several Modules, that can appear in Regions of a Shell module. The Modules communicate with each other via an EventAggregator, and that’s all ok.
One of the Modules is a MainMenu, and that publishes Events when a menu-item is clicked. Other Modules react to that via subscription, hence the TitleBarModule shows the name-property of the chosen menu-item. That’s
the framework and it works. What I would like to add now, is a ModuleManager, that would also react to the Events from the MainMenuModule. The responsibility of the ModuleManager would be to load the requested Module.
The code I use for the Modules is – following MVVM principles – structured as follows: every View has the responsibility to instantiate it’s
ViewModel with an instruction like this:
The View has no other codebehind than its own IntializeComponent. That fires up the ViewModel allright. All the ViewModels need the EventAggregator (and
some more shared functionality, like e.g. the NotifyPropertyChanged) so I have a BaseViewModel, from which they all Inherit. Here’s the relevant part of that BaseViewModel:
And this is how the ViewModel inherits and is instantiated:
If Not IsDesignTime
Private Sub SubscribeToEvents ()
Private Sub ReactOnMenuChoice (ByVal MenuChoice
If Not MenuChoice =
Select Case MenuChoice
‘##### is replaced by concrete MenuNames of course!
As soon as the SatisfyImports is hit, this is the resulting error, pointing to the XAML at the location that is quoted above in this email:
1) No valid exports were found that match the constraint '((exportDefinition.ContractName == "Microsoft.Practices.Prism.Modularity.IModuleManager") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity")
AndAlso "Microsoft.Practices.Prism.Modularity.IModuleManager".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected." & vbCrLf & "" & vbCrLf & "Resulting in: Cannot set import 'Shell.StartViewModel.Locale_ModuleManager
(ContractName="Microsoft.Practices.Prism.Modularity.IModuleManager")' on part 'Shell.StartViewModel'." & vbCrLf & "Element: Shell.StartViewModel.Locale_ModuleManager (ContractName="Microsoft.Practices.Prism.Modularity.IModuleManager") -->
Shell.StartViewModel" & vbCrLf & "" & vbCrLf & " bij System.ComponentModel.Composition.CompositionResult.ThrowOnErrors(AtomicComposition atomicComposition)" & vbCrLf & " bij System.ComponentModel.Composition.Hosting.ComposablePartExportProvider.Compose(CompositionBatch
batch)" & vbCrLf & " bij System.ComponentModel.Composition.Hosting.CompositionContainer.Compose(CompositionBatch batch)" & vbCrLf & " bij System.ComponentModel.Composition.CompositionInitializer.SatisfyImports(ComposablePart
part)" & vbCrLf & " bij System.ComponentModel.Composition.CompositionInitializer.SatisfyImports(Object attributedPart)" & vbCrLf & " bij Shell.StartViewModel..ctor()" & vbCrLf & " --- Einde van intern
uitzonderingsstackpad ---" & vbCrLf & " bij System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)" & vbCrLf & " bij Shell.StartView.InitializeComponent()" & vbCrLf & " bij
I read from this that there were
no valid Exports found in the IModuleManager??? What am I missing here, of do I need to refer to anything else/more???
When I leave out the import of the IModuleManager in the Base_ViewModel, everything works fine – except of course that I do not have a ModuleManager.
Should the ModuleManager be instantiated differently?
I have also tried to instantiate the ViewModel from the View’s code behind, but that gives another strange effect: When I do that, the import of the EventAggregator and the ModuleManager has to move to the ViewModel,
because it cannot be inherited like in all the other Modules. That gives a non-responsive EventAggregator (another instance???) so no reaction after subscription. And thus the ReactOnMenuChoice is never hit (while all other modules still perfectly react!)
There are some more variants I have been experimenting with, but they all come to the same or comparable – and disappointing - results.
I am anxious to get to know the truth behind this strange behavior of the IModuleManager.