Where is the right place for resource used by controls in module when using prism?

Normally, all resources are put in app.xaml, 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?

13.10.2009 15:13:29
2 ОТВЕТА

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...
1
13.10.2009 16:19:22

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

Composite WPF (Prism) module resource data templates

2
23.05.2017 12:13:37
+1 Damn Anderson, I gotta be quick on the draw to compete with you on these Prism questions!
Jeremiah Morrill 13.10.2009 22:22:47
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 14.10.2009 19:02:59
@Anderson - too bad not more of us are using Prism around here... you'd be the jon skeet of prism ;)
Metro Smurf 15.10.2009 04:08:40