How does a module project access the main application's style resources?

Topics: Prism v4 - WPF 4
May 23, 2013 at 8:49 PM
I've been searching high and low to figure out a way for my class library project (containing WPF views) can simply access Styles and other resources from the main application's App.xaml. Everything I've found on the web so far prescribes some explicit technique (e.g. using Pack URIs) to accomplish this. However, I've studied the StockTraderRI app and somehow it accomplishes this without using any of these other techniques. As an example, the Styles.xaml, from the StockTraderRI project, defines a TextBlockSectionTitle style. And seemingly effortlessly, the ArticleView.xaml code (see Text="NEWS ARTICLES") in the StockTraderRI.Modules.News project accesses this style. No URIs or other tricks are used (that I can see).

I have not been able to reproduce this perfectly ideal scenario in a test application. Does anyone know how PRISM does this?

May 24, 2013 at 5:53 PM
As far as I know, this is not an specific feature of Prism but of WPF itself.

Based on my understanding, during run-time, the resources defined in the App.xaml file of the main project become available for any component of the application (any resource defined in other App.xaml files of other projects will be ignored.) Regardless from where the components are defined (main project, class library, etc...) they form part of the running application and therefore they will use the resources defined in it.

However, this might differ during design-time when using the designer. Based on my understanding, it seems that if the main project has a reference to the module (like the StockTraderRI project to the StockTraderRI.Modules.News module) and said module is a class library, visual studio assumes that the main project is the entry point of the application and it loads its resources when using the designer, allowing you to see the effects of the style in design-time. You can check this by removing the reference in the StockTraderRI project, cleaning the solution and restarting visual studio. You should see that the ArticleView no longer finds the TextBlockSectionTitle style.

The result of this is that, if you have a module that is not directly referenced by your main project and is loosely loaded, then you would not be able to see the effects of using a style or resource in it that was defined in the main project during design-time. However, those should be correctly applied during run-time in the components of the module.

Regarding this subject, I believe you could find the following article of the Knowledge Base useful:
I hope this helps,

Damian Cherubini
May 24, 2013 at 6:02 PM
Thanks Damian, your suggestion solved my problem: all I needed to do was add the project reference and restart Visual Studio. Thanks also for that link.