KentZhou
KentZhou

Reputation: 25583

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?

Upvotes: 1

Views: 1153

Answers (2)

Anderson Imes
Anderson Imes

Reputation: 25650

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

Composite WPF (Prism) module resource data templates

Upvotes: 2

Konamiman
Konamiman

Reputation: 50323

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...

Upvotes: 1

Related Questions