View not found when using MEF and DirectoryCatalog

Sep 7, 2010 at 4:58 PM

I ran into a problem this weekend were I was trying to create a WPF application with MEF and Micro.Caliburn and I was wondering if anyone else has experienced a similar issue.  I have a ViewModel and a View in a different assembly that is placed in an Extensions folder that MEF's DrectoryCatalog is pointing to.  When my application spins up the ViewModel is found just fine and is instantiated.  However when the View is trying to be resolved by Caliburn I get the MyView not found message that is displayed in the ContentControl in place of the actual view.  If I place the ViewModel and View in the executing assembly then the view displays fine, so I believe it has to do with the fact that I am attempting to dynamically load the ViewModel and View in an unreferenced assembly.

I need to create a sample application showing this behavior before I can show some code snippets of what I am trying to do, but I wanted to get the discussion going first.

 

Thanks

Mike  

Coordinator
Sep 7, 2010 at 5:22 PM

You need to make sure that your Views assembly is added to AssemblySource.Instance. If the assembly is statically referenced, you can do this by overriding SelectAssemblies in the Bootstrapper and making sure to pass all assemblies that contain views or VMs. If it is dynamically loaded, you can just added it to AssemblySource.Instance directly... though I'm not sure if there is a way to get this information out of the DirectoryCatalog. I haven't tried that before. As an alternative, you can replace the ViewLocator.LocateViewForModelType with some custom logic that knows how to locate your views.

Sep 7, 2010 at 9:43 PM

Thanks Rob your suggestions got me on the right path.  I wasn't able to use DirectoryCatalog as it doesn't appear to expose the loaded assemblies.  Here is what I did in my BootStrapper:

Override the SelectAssemblies and dynamically loaded the assembly located in my Extensions subfolder (used Assembly.LoadFrom).  I made sure the SelectAssemblies returned the executing assembly as well as the dynamically loaded one. The code I had in the overriden Configure method already created AssemblyCatalogs for the assemblies that are part of the AssemblySource.Instance so it just worked :)

 

Mike 


Dec 17, 2010 at 8:45 PM

Mike, can I see how you did that?