2010-01-25 76 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