DNN模块间通信Inter Module Communication(IMC)(转自http://www.dnnsun.com)
首先,先简单介绍一下何谓IMC, IMC的全称是Inter Module Communication,它主要负责模块之间的通信.
Type:可自定义的类型标识,它主要表示当前IMC的一个认证标识,好比IMC的发送者和接收者的一个默认信号,凭借它可以把发送者和接收者牵连起来,如此接受者只监听属于它自己类型的事件.
Sender:顾名思义,这就是事件消息的发送者.
Target:事件消息的目标,也就是接收者.
Value:这是一个返回对象类型的属性,也就是IMC的重点所在,也就是你需要传递的参数,它可以是任何复杂的对象类型,比如说Dataset,Control等等,只要该事件的接收者能够解析它.
Text:可选的参数,一个简单的字符串,你可设置附加的参数信息.
消息发送:
//引用namespace
using DotNetNuke.Entities.Modules.Communications;
//继承IModuleCommunicator
public partial class List : PortalModuleBase, IActionable, IModuleCommunicator
//注册event
public event ModuleCommunicationEventHandler ModuleCommunication;
//按钮单击
protected void Button1_Click(object sender, EventArgs e)
{
try
{
//To raise the event for the intermodule communication you need this code
//Event arguments to send to listeners
ModuleCommunicationEventArgs args = new ModuleCommunicationEventArgs();
//Event properties (optional). Use whatever properties you need.
//Property Text: Some text you want to send
args.Text = "Some text";
//Property Sender: Sender's Identification. Can be the name of the module starting the communication
//Can be used to filter events originating from a specific module
args.Sender = "Link";
//Property Target: Target's Identification. Can be the name of the module to whom this event is sent
//Can be used to filter events directed to a specific module
args.Target = "BLOG";
//Property Value: Any object you want to send to the listeners. You can put whatever object you need in this variable
args.Value = "A String Object";
//Property Type: A type specification. Can be used to filter event senders
args.Type = this.GetType().ToString();
//Raise the event (use the same name as defined above)
if (ModuleCommunication != null)
{
ModuleCommunication(this, args);
}
}
catch
{
}
}
监听:
//添加namespace
using DotNetNuke.Entities.Modules.Communications;
//页面继承IModuleListener
public partial class List : PortalModuleBase, IActionable,IModuleListener
//监听
public void OnModuleCommunication(object s, ModuleCommunicationEventArgs e)
{
Label1.Text = e.Text.ToString();
}
浙公网安备 33010602011771号