views:

38

answers:

2

Lets say I have a WPF application that shows a ListBox with an ArrayList -populated with objects of arbitrary types- as a source, and this application is hosted in an assembly 'A'. By default the ListBox will display the custom object 'ToString' method return value. If a data template for that object type is found, the ListBox will use it for rendering. Imagine that theres another assembly 'B' that references of 'A' and seeks to extend it by providing custom data templates for certain types, to be used in that ListBox. Is there some way to do that without 'A'being aware of B?

+2  A: 

Take a look on Composite Application Guidance. It should work for you.

Hun1Ahpu
While this is not a direct answer to my question, it will be very useful, thanks
Thiado de Arruda
+1  A: 

Yes, this is a very common usage of WPF.

In your assembly B:

  1. Create a Themes folder containing a file named Generic.xaml containing an empty <ResourceDictionary> tag
  2. Inside the <ResourceDictionary> tag, add DataTemplates and ControlTemplates for the types in B
  3. In your AssemblyInfo.cs file, add the following line:

    [assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

Now in your assembly A:

  1. Create a UI to browse to assembly B (or otherwise select the .dll file)
  2. When the user selects an assembly B to use, use var assembly = Assembly.LoadFile(path) to load it
  3. Use Activator.CreateInstance(assembly.GetType(typeName)) to create an object in assembly B knowing only its name
  4. Add this object to your UI, or create other objects using an interface defined on it (the interface itself is defined in assembly A) and add the to your UI

The templates defined in assembly B will be used to present the controls and data in assembly B, even though assembly A knows nothing of assembly B.

Ray Burns