开发者

How do I extend a WPF application using an addin-like architeture?

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 'ToStri开发者_JS百科ng' 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜