views:

249

answers:

2

Normally, all resources are put in app.xmal, or other resources xaml files(as resource dictionary) and then reference it in app.xaml.

When applying prism pattern, for those module, there is no app.xaml file. Application class is replaced by a class implementing interface IModule. So where is the right place for resources used by controls in the module?

+1  A: 

You can add the resource dictionary in the same assembly as the module or in other loaded assembly, then access it programmatically by using the Application.GetResourceStream method; however you will need to know the name of the assembly where the resource is defined.

For example, if your assembly is named TheAssembly and the resource dictionary is named TheDictionary.xaml, you can do (Disposes not shown for brevity):

StreamResourceInfo sr = Application.GetResourceStream(
  new Uri("/TheAssembly;component/TheDictionary.xaml", UriKind.Relative));

StreamReader r=new StreamReader(sr.Stream);
string xaml=r.ReadToEnd();

ResourceDictionary rd = (ResourceDictionary)XamlReader.Load(xaml);

From here, you can for example use the Unity container to make the resource dictionary available application-wide.

UPDATE

The following version avoids having to hard-code the assembly name, provided that the resource is in the currently executing assembly:

string assemblyName = Assembly.GetExecutingAssembly().FullName.Split(',')[0];
string uri = string.Format("/{0};component/Dictionary1.xaml", assemblyName);

StreamResourceInfo sr = Application.GetResourceStream(
  new Uri(uri, UriKind.Relative));

//etc...
Konamiman
+2  A: 

this is how i do it: have modules register resources with the app.

http://stackoverflow.com/questions/1172171/composite-wpf-prism-module-resource-data-templates/1172794#1172794

Anderson Imes
+1 Damn Anderson, I gotta be quick on the draw to compete with you on these Prism questions!
Jeremiah Morrill
I know Prism pretty well, so I try and answer these as much as I can. Anything else is really just a shot in the dark :)
Anderson Imes
@Anderson - too bad not more of us are using Prism around here... you'd be the jon skeet of prism ;)
Metro Smurf