2010-01-25 49 views
3

我正在为我公司的内部业务系统试验插件体系结构。我设法读取实现特定接口的Plugin文件夹中的所有.DLL。我试图弄清楚的是“宿主”MDI父应用程序与将要放入.DLL中的打算制作MDI子项的表单之间的最佳通信方法。插件体系结构 - 使MDI父窗体意识到DLL中的儿童

目前,我只返回来自.DLLs的ToolStripMenuItem对象以添加到MDI Parent。我还测试了将.DLL中连接到ToolStripMenuItems的事件传播到.DLL中的代码。我还设法通过界面返回一个Form对象并打开该窗体,因为Plugin文件夹正在被“扫描”。

但是,我不清楚我将如何使这些形式的MDI儿童。此外,生活在.DLL中的任何其他形式也必须是MDI儿童。我创建了一个VS 2008 Addin项目来查看正在发生的事情,看起来Addin接受一个Application对象,它将它添加到ToolStripMenuItems并执行其他操作。在.DLL中构建菜单的代码。这与我迄今所做的相反,即MDI从每个.DLL请求一个ToolStripMenuItem,并将返回的对象添加到它自己的菜单中。

设计我的插件体系结构以相同的方式接受应用程序对象唯一的方法,我可以让窗体打开为MDI孩子?我是否通过不将应用程序对象传递给.DLL来请求其他的,目前我不知道的头痛?

+0

我很想做类似的事情。你有没有得到这个工作?我不确定如何让主机窗体监控一个文件夹,一旦这样做,如何让它检查正确的界面来添加一个插件。你介意发布,如果不是你的代码的链接,那么可能是你如何得到这个工作的示例代码? – 2010-07-07 21:42:04

回答

4

几年前我们做了类似的事情。我们如何处理它是通过创建一个由PluginManager和Plugins实现的几个接口。

的插件管理器中实现类似这样的界面:

''' <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 

插件实现了这个接口:

''' <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然后初始化并运行插件。

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

这是一个非常简单的合同,您可以轻松更改或扩展两者。

+0

你介意发布一个实现接口的子窗体的例子吗? – 2010-07-07 21:43:29