Hi,
Based on the code snippets mentioned above, I believe that the exception you mentioned could be caused if thenewView instance is not being correctly resolved from the container by theServiceLocator in the above line:
object newView = ServiceLocator.Current.GetInstance<ISubsystemDetailsView>();
For example you could check if you are correctly exporting the SubsystemDetailsViewclass to the container and mapping it to the corresponding interface for example using the following attributes:
[Export(typeof(ISubsystemDetailsView))]
[PartCreationPolicy(CreationPolicy.NonShared)]
Also, the view model should be exported similarly if it will be imported using theISubsystemDetailsViewModel VM property like you mentioned.
Additionally, I found that in this second block the view added and activated should be thenewView instance and not the existingView. Also, if your view model is being imported to the view using theISubsystemDetailsViewModel, then the DataContext should be cast to this interface and not the specific implementation, for example this could look like this:
if (!alreadyExists) {object newView = ServiceLocator.Current.GetInstance<ISubsystemDetailsView>(); subsysdetailsViewModel = ((FrameworkElement)newView).DataContext as ISubsystemDetailsViewModel; subsysdetailsViewModel.Subsystem = Subsys; mainRegion.Add(newView); mainRegion.Activate(newView); }
On the other hand regarding the UI Composition approach to use, I don't think that the view discovery approach should be suitable in this case, as based on my understanding of your scenario you will need programmatic control over when the view is created and displayed in the region in which case using view injection may be more appropriate. You could find more information on when to use one approach or the other in the following chapter of the documentation:
I hope this helps,
Agustin Adami
http://blogs.southworks.net/aadami