Apr 7, 2009 at 10:19 PM
I have a WPF application in which I am using MEF to compose the system. I have run into a problem though since it appears that doing this breaks the connection between windows and the application resource dictionary. In other words, there is no longer a central place in which I can define styles etc. that will apply to the entire application.

I have stepped into WPF source code for both a "normal" WPF application as well as an MEF-based one, and it appears that the former ends up retrieving the resource dictionary from Application class (via the Resources property) whereas the latter never does. This problem exists even if the application and window are in the same assembly.

Does anyone have any idea on how to get this application-level resource dictionary hooked up again?

Apr 7, 2009 at 11:41 PM
Never mind - I figured it out. Just for the sake of the next person to try this:

When you use MEF to compose a WPF application, you end up removing the "StartupURI=xxx" line from the App.xaml file because you want MEF to create the main window for you. You end up with an app.xaml file that looks like this:



Application x:Class="YourMainApp"
                ... your resources here

The problem with this is that VS does not generate an InitializeComponent() method for the YourMainApp class if there is nothing but namespace declarations specified for the class. InitializeComponent() is where the resources get loaded. I don't know if there is a more elegant way to cause this code to be emitted, but I found that attaching any kind of event handler (i.e. Startup=SomeDummyHandler) causes InitializeComponent() to be generated and called. Problem solved...