如何使用类似外接程序的架构来扩展 WPF 应用程序?
假设我有一个 WPF 应用程序,它显示一个 ListBox,其中包含一个 ArrayList(填充有任意类型的对象)作为源,并且该应用程序托管在程序集“A”中。默认情况下,ListBox 将显示自定义对象“ToString”方法的返回值。如果找到该对象类型的数据模板,ListBox 将使用它进行渲染。 想象一下,有另一个程序集“B”引用“A”,并试图通过为某些类型提供自定义数据模板来扩展它,以便在该列表框中使用。有什么方法可以在“A”不知道 B 的情况下做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是 WPF 的一个非常常见的用法。
在程序集 B 中:
标记
标记内,添加 DataTemplates 和 ControlTemplates对于 B 中的类型在 AssemblyInfo.cs 文件中,添加以下行:
[程序集:ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
现在在您的程序集 A:
程序集 B 中定义的模板将用于呈现程序集 B 中的控件和数据,即使程序集 A 对程序集 B 一无所知。
Yes, this is a very common usage of WPF.
In your assembly B:
<ResourceDictionary>
tag<ResourceDictionary>
tag, add DataTemplates and ControlTemplates for the types in BIn your AssemblyInfo.cs file, add the following line:
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
Now in your assembly A:
var assembly = Assembly.LoadFile(path)
to load itActivator.CreateInstance(assembly.GetType(typeName))
to create an object in assembly B knowing only its nameThe 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.
请查看复合应用程序指南。它应该适合你。
Take a look on Composite Application Guidance. It should work for you.