一个实现自定义event的文章。。。我还没有完全摸透。。不知道有没人有兴趣。。新手就不用看了,先学会走...
p;}
}
}
public class MessagingHandler
{
private string strMessage;
public string EventMessage
{
get
{
return strMessage;
}
set
{
strMessage = value;
}
}
public void GetMessageData(object sender, MessageEventArgs e)
{
string strMessage = e.MessageText;
EventMessage = strMessage;
}
}
The class MessageEventArgs will supply the data for event. Notice that it is inherited from EventArgs. As you can see MessageText is a property of the class and a get statement is utilized to return the data string. The delegate in this case is the MessageHandler. It has the sender object and the MessageEventArgs for the data in its argument list. Moving onto the class MessageEvent. This class will raise the event with the OnMessage() method by invoking the delegates. The MessagingHandler class will extract the event message from the MessageEventArgs and populate the EventMessage property so the message can be displayed on the Web page.
In summarization, we have a class that supplies the data, a delegate to provide the communication, a class to raise the event, and a class to handle the event. The basic workflow is as follows:
- The InitEvent() is initiated from the Page_Load() method when the page is requested.
- MessagingHandler mh = new MessagingHandler() instantiates the MessagingHandler class - Event Receiver.
- MessageEvent me = new MessageEvent() instantiates the MessageEvent class - Event Sender.
- me.Message += new MessageHandler(mh.GetMessageData) registers the event handler with the event source so that the MessagingHandler instance can receive alarm events. The += assignment operator adds the delegate to the list of delegates that are registered with the Message event.
- When the specified value is reached, MessageEventArgs e = new MessageEventArgs() is instantiated and the OnMessage() is called to raise the event.
- The OnMessage() method invokes the delegate which calls the GetMessageData() method to handle the event.
- The GetMessageData() method obtains the message text from the MessageEventArgs and populates the EventMessage property with the string data.
}
}
public class MessagingHandler
{
private string strMessage;
public string EventMessage
{
get
{
return strMessage;
}
set
{
strMessage = value;
}
}
public void GetMessageData(object sender, MessageEventArgs e)
{
string strMessage = e.MessageText;
EventMessage = strMessage;
}
}
The class MessageEventArgs will supply the data for event. Notice that it is inherited from EventArgs. As you can see MessageText is a property of the class and a get statement is utilized to return the data string. The delegate in this case is the MessageHandler. It has the sender object and the MessageEventArgs for the data in its argument list. Moving onto the class MessageEvent. This class will raise the event with the OnMessage() method by invoking the delegates. The MessagingHandler class will extract the event message from the MessageEventArgs and populate the EventMessage property so the message can be displayed on the Web page.
In summarization, we have a class that supplies the data, a delegate to provide the communication, a class to raise the event, and a class to handle the event. The basic workflow is as follows:
- The InitEvent() is initiated from the Page_Load() method when the page is requested.
- MessagingHandler mh = new MessagingHandler() instantiates the MessagingHandler class - Event Receiver.
- MessageEvent me = new MessageEvent() instantiates the MessageEvent class - Event Sender.
- me.Message += new MessageHandler(mh.GetMessageData) registers the event handler with the event source so that the MessagingHandler instance can receive alarm events. The += assignment operator adds the delegate to the list of delegates that are registered with the Message event.
- When the specified value is reached, MessageEventArgs e = new MessageEventArgs() is instantiated and the OnMessage() is called to raise the event.
- The OnMessage() method invokes the delegate which calls the GetMessageData() method to handle the event.
- The GetMessageData() method obtains the message text from the MessageEventArgs and populates the EventMessage property with the string data.
| 对此文章发表了评论 |

