.net - 插件架构 - 使 MDI 父窗体意识到 DLL 中的子项

标签 .net vb.net plugins architecture

我正在为我公司的内部业务系统试验插件架构。我设法读取了实现特定接口(interface)的插件文件夹中的所有 .DLL。我想弄清楚的是“主机”MDI 父应用程序和将在我打算制作 MDI 子级的 .DLL 中的表单之间的最佳通信方法。

目前,我只从 .DLL 返回 ToolStripMenuItem 对象以添加到 MDI 父级。我还测试了在 .DLL 中连接到 ToolStripMenuItems 的事件确实会传播到 .DLL 中的代码。我还设法通过界面返回一个 Form 对象并打开该表单,因为正在“扫描”Plugin 文件夹。

但是,我不清楚如何使这些窗体成为 MDI 子窗体。此外,.DLL 中的任何其他形式也必须是 MDI 子级。我创建了一个 VS 2008 Addin 项目只是为了看看发生了什么,看起来 Addin 接受一个 Application 对象,它在该对象上添加到 ToolStripMenuItems 并执行其他操作。在 .DLL 中构建菜单的代码。这与我目前所做的相反,在 MDI 中,MDI 从每个 .DLL 请求一个 ToolStripMenuItem,并将返回的对象添加到它自己的菜单中。

将我的插件架构设计为接受应用程序对象的方式是否与我可以作为 MDI 子进程打开表单的唯一方式相同?如果不将应用程序对象传递给 .DLL,我是否会遇到其他目前未知的问题?

最佳答案

很多年前我们做过类似的事情。我们如何处理它是通过创建几个由 PluginManager 和插件实现的接口(interface)。

插件管理器实现了一个与此类似的接口(interface):

    ''' <summary>
'''The IPluginManager interface is implemented by whatever component manages your gui
'''It provides a means for plugins to access GUI elements of the application
''' </summary>
Public Interface IPluginManager

    ''' <summary>
    '''The MDIForm property allows the plugin to display itself
    '''inside of the application's main MDI form (ie.  plugin.form.mdiparent=mdiform)
    ''' </summary>
    ReadOnly Property MDIForm() As Form
    ReadOnly Property Toolbar() As ToolBar

    ''' <summary>
    '''Allows the plugin to request that the application's main menu be updated
    ''' </summary>
    ''' <param name="Menu">The menu to add to the main menu</param>
    Sub UpdateMainMenu(ByVal Menu As Menu)

    ''' <summary>
    '''Allows the plugin to request that the application's workspace toolbar be updated
    ''' </summary>
    ''' <param name="Buttons">the collection of toolbar buttons to add to the toolbar</param>
    Sub UpdateToolbarButtons(ByVal Buttons As ToolBar.ToolBarButtonCollection)

End Interface

插件实现了这个接口(interface):

    ''' <summary>
'''The IPlugin interface is implemented by all plugins
'''It provides a standardized means for the pluginmanager
'''to communicate with a plugin, without knowing the plugin explicitly
''' </summary>
Public Interface IPlugin

    ''' <summary>
    '''Allows the plugin to be intialized first before it asked to run
    ''' </summary>
    Sub Initialize()

    ''' <summary>
    '''Allows the pluginmanager to register itself with the plugin
    '''so the plugin can listen to events from the pluginmanager
    ''' </summary>
    ''' <param name="PluginManager">A plugin manager that implements the IPluginManager interface</param>
    Sub RegisterPluginManager(ByVal PluginManager As IPluginManager)

    ''' <summary>
    '''Requests the plugin to run its functionality
    ''' </summary>
    Sub Run()

End Interface

应用程序启动后,PluginManager 会找到所有可用的插件(听起来您已经可以正常工作了),然后实例化每个插件,并在每个插件中注册自己。 PluginManager 然后初始化然后运行插件。

在插件方面,当插件初始化或运行时(取决于您的需要),您只需将插件表单的 MDIParent 属性设置为 IPluginManager 接口(interface)中提供的 MDIForm。还有中提琴!

这是两者之间的一个非常简单的契约(Contract),您可以轻松更改或扩展。

关于.net - 插件架构 - 使 MDI 父窗体意识到 DLL 中的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2135940/

相关文章:

c# - 在 C# 中使用重载的 VB.NET Not 运算符

c# - 如何使用旧的 MS Sans Serif 字体

python - 如何使用 sublime 插件 api 创建新布局并在每个单元格中打开一个文件

grails - 可搜索的插件不允许创建目录

c# - 如何解决用于测试目的的基本 Controller 依赖注入(inject)?

.net - 实例化 .Net COM 可见类时出现自动化错误

c# - 为 MVC3 和 MVC4 Beta 编写自定义 HtmlHelpers,我应该使用 HtmlString 还是 MvcHtmlString?

vb.net - 有没有办法将表单放置在任何尺寸屏幕上的相同位置?

c# - 如何获取图像的一部分并将其用作单独的图像?

plugins - 某些推文的 WordPress 插件