How do I extend a WPF application with an addik-like architecture?
Let's say I have a WPF application that shows a ListBox with an ArrayList populated with arbitrary object types as its source, and that application is hosted in assembly "A". By default, the ListBox will display the return value of the "ToString" method of the custom object. If a data template is found for this type of object, the ListBox will use it for rendering. Imagine that theres another assembly "B" that references "A" and is trying to extend it by providing custom data templates for specific types that will be used in this ListBox. Is there a way to do this without the "A" to know about B?
a source to share
Yes, this is a very common use of WPF.
In your assembly B:
- Create a Themes folder containing the Generic.xaml file containing an empty tag
<ResourceDictionary>
- In tag
<ResourceDictionary>
add DataTemplates and ControlTemplates for types in B -
In the AssemblyInfo.cs file, add the following line:
[assembly: ThemeInfo (ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
Now in your assembly A:
- Build a UI to navigate to assembly B (or else select the .dll file)
- When user selects assembly B to use, use
var assembly = Assembly.LoadFile(path)
to load it - Use
Activator.CreateInstance(assembly.GetType(typeName))
to create an object in assembly B knowing only its name - Add this object to your UI, or create other objects using the interface defined on it (the interface itself is defined in assembly A) and add to your UI
The templates defined in assembly B will be used to represent controls and data in assembly B, even though assembly A knows nothing about assembly B.
a source to share