1.
具体实现,如下:
■link模块(事件引发者)
1. 声明Communications 的命名空间
Imports DotNetNuke.Entities.Modules.Communications
2. 需要指定IModuleCommunicator接口
Public MustInherit Class Links
Inherits Entities.Modules.PortalModuleBase
Implements IModuleCommunicator
Implements Entities.Modules.IActionable
3. 定义事件
Public Event ModuleCommunication(ByVal sender As Object, ByVal e As ModuleCommunicationEventArgs) Implements IModuleCommunicator.ModuleCommunication
4. 在btnSend按钮事件中发送ModuleCommunication事件
1
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
2
Try
3
'To raise the event for the intermodule communication you need this code
4
'Event arguments to send to listeners
5
Dim args As New ModuleCommunicationEventArgs
6
'Event properties (optional). Use whatever properties you need.
7
'Property Text: Some text you want to send
8
args.Text =
"Some text"
9
'Property Sender: Sender's Identification. Can be the name of the module starting the communication
10
'Can be used to filter events originating from a specific module
11
args.Sender =
"Link"
12
'Property Target: Target's Identification. Can be the name of the module to whom this
event
is sent
13
'Can be used to filter events directed to a specific module
14
args.Target =
"BLOG"
15
'Property Value: Any object you want to send to the listeners. You can put whatever object you need in this variable
16
args.Value =
"A String Object"
17
'Property Type: A type specification. Can be used to filter event senders
18
args.Type = Me.GetType.ToString()
19
'Raise the event (use the same name as defined above)
20
RaiseEvent ModuleCommunication(Me, args)
21
Catch exc As Exception
22
ProcessModuleLoadException(Me, exc)
23
End Try
24
End Sub
25
■blog模块(事件监听者)
1. 声明Communications 的命名空间
Imports DotNetNuke.Entities.Modules.Communications
2. 需要指定IModuleListener接口
Public MustInherit Class BLOG
Inherits Entities.Modules.PortalModuleBase
Implements IModuleListener
Implements Entities.Modules.IActionable
Implements Entities.Modules.IPortable
Implements Entities.Modules.ISearchable
3. 实现事件处理程序
Public Sub OnModuleCommunication(ByVal s As Object, ByVal e As ModuleCommunicationEventArgs) Implements IModuleListener.OnModuleCommunication
Me.txtListen.Text = e.Text.ToString()
End Sub
2.原理
IMC的实现在ModuleCommunication.vb文件中。
IMC存在两个list,一个存放所有事件引发者ModuleCommunicators,一个存放所有事件监听者ModuleListeners
每个模块在加载时,都会调用LoadCommunicator()函数,检查该模块是否实现了IModuleCommunicator接口或者是IModuleListener接口。如果实现了,便加入到上面提到的对应的list中。
1
Public Sub LoadCommunicator(ByVal ctrl As System.Web.UI.Control)
2
System.Diagnostics.Debug.WriteLine("LoadCommunicator")
3
' Check and see if the module implements IModuleCommunicator
4
If TypeOf ctrl Is IModuleCommunicator Then
5
Me.Add(CType(ctrl, IModuleCommunicator))
6
End If
7
8
' Check and see if the module implements IModuleListener
9
If TypeOf ctrl Is IModuleListener Then
10
Me.Add(CType(ctrl, IModuleListener))
11
End If
12
13
End Sub
并利用AddHandler将事件与事件处理程序相关联。
For i = 0 To _ModuleListeners.Count - 1
AddHandler item.ModuleCommunication, AddressOf _ModuleListeners(i).OnModuleCommunication
Next i
由于IMC是广播式发布事件和事件监听。所以对于某个事件是否在该事件处理程序中进行处理,需要在事件处理程序中自行根据参数进行鉴别。