介绍在DNN
1. 通信示例
现在假设页面中,点击link模块中的btnSend按钮后,需要将用户输入的text信息发送到blog模块中进行处理,即blog模块需要接收到用户输入的text信息。这里就涉及到link模块和blog模块间的通信问题。
具体实现,如下:
■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事件
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click2
Try3
'To raise the event for the intermodule communication you need this code4
'Event arguments to send to listeners5
Dim args As New ModuleCommunicationEventArgs6
'Event properties (optional). Use whatever properties you need.7
'Property Text: Some text you want to send8
args.Text = "Some text"9
'Property Sender: Sender's Identification. Can be the name of the module starting the communication10
'Can be used to filter events originating from a specific module11
args.Sender = "Link"12
'Property Target: Target's Identification. Can be the name of the module to whom this event is sent13
'Can be used to filter events directed to a specific module14
args.Target = "BLOG"15
'Property Value: Any object you want to send to the listeners. You can put whatever object you need in this variable16
args.Value = "A String Object"17
'Property Type: A type specification. Can be used to filter event senders18
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 Exception22
ProcessModuleLoadException(Me, exc)23
End Try24
End Sub25

■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中。
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 Then5
Me.Add(CType(ctrl, IModuleCommunicator))6
End If7

8
' Check and see if the module implements IModuleListener 9
If TypeOf ctrl Is IModuleListener Then10
Me.Add(CType(ctrl, IModuleListener))11
End If12

13
End Sub
并利用AddHandler将事件与事件处理程序相关联。
For i = 0 To _ModuleListeners.Count - 1
AddHandler item.ModuleCommunication, AddressOf _ModuleListeners(i).OnModuleCommunication
Next i
由于IMC是广播式发布事件和事件监听。所以对于某个事件是否在该事件处理程序中进行处理,需要在事件处理程序中自行根据参数进行鉴别。