在运行时创建 WPF 窗口
是否可以在运行时创建 WPF 窗口控件?
我正在考虑一个有点 hack 式的问题解决方案,但我本质上希望能够有一个 dll,它允许我动态加载 WPF 窗口并编译它的关联代码(分别来自 xaml 和 cs 文件) )。我假设我还必须手动加载代码隐藏所需的任何程序集。
这将使我能够创建模块化插件,允许 Windows 从另一个应用程序运行,而无需编译到源代码中。
有一些关于这个主题的帖子,但似乎没有一个专门处理窗口/代码创建。
如果不可能(或者只是真的不推荐),我们将不胜感激关于替代方案的想法。
干杯,
凯尔
Is it possible to create a WPF Window control at runtime?
I'm thinking of a somewhat hack-ish solution to a problem, but I would essentially like to be able to have a dll which allows me to dynamically load a WPF Window and compile it's associated code behind (from an xaml and cs file respectivly). I assume I would also have to manually load any assemblies that the codebehind requires.
This would give me the ability to create modular addons which allow for Windows to be run from another application without being compiled into the source.
There are a few SO posts about this topic, but none seem to deal with window/code creation specifically.
If it's not possible (or just really not reccomended) thoughts as to alternatives would be greatly appreciated.
Cheers,
Kyle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您研究一下 MEF 等技术。现在它直接包含在框架中,但允许您将第 3 方代码/windows/views/viewmodels 直接“注入”到您的应用程序中,而无需分发您的应用程序。
您可以提供简单的接口,然后让 MEF 为您编写应用程序,而不是尝试在应用程序中编译代码。这是一个更容易、更灵活的选择。
我之前使用过这个,我有一个 ViewModel 合同和一个包含 DataTemplates 的 ResourceDictionaries 合同。这允许您对其关联的视图进行逻辑(ViewModel)的完全动态运行时注入,并在运行时将其包装在窗口中。
I would recommend looking into technologies like MEF instead. This is now included directly in the Framework, but allows you to "inject" 3rd party code/windows/views/viewmodels directly into your application, without distributing your application.
Instead of trying to compile the code in your application, you can provide simple interfaces, and let MEF compose the application for you. This is a much easier, more flexible option.
I've used this before where I've had a contract for ViewModels and a contract for ResourceDictionaries containing DataTemplates. This allows you to do fully-dynamic runtime injection of logic (ViewModel) with their associated Views, and just wrap it up in a Window at runtime.