什么是事件?

mengmou 2007-05-23 09:22:48
这几天在学C#,主要就是看MSDN,事件的用法已经会了,可事件的机制总是看不明白,劳烦大家帮我解释一下下面这个事件是如何定义、触发的,每行代码背后的含义是什么,谢谢!


namespace TestCollections
{
// A delegate type for hooking up change notifications.
public delegate void ChangedEventHandler(object sender, System.EventArgs e);

// A class that works just like ArrayList, but sends event
// notifications whenever the list changes.
public class ListWithChangedEvent : System.Collections.ArrayList
{
// An event that clients can use to be notified whenever the
// elements of the list change.
public event ChangedEventHandler Changed;

// Invoke the Changed event; called whenever list changes
protected virtual void OnChanged(System.EventArgs e)
{
if (Changed != null)
{
Changed(this, e);
}
}

// Override some of the methods that can change the list;
// invoke event after each
public override int Add(object value)
{
int i = base.Add(value);
OnChanged(System.EventArgs.Empty);
return i;
}

public override void Clear()
{
base.Clear();
OnChanged(System.EventArgs.Empty);
}

public override object this[int index]
{
set
{
base[index] = value;
OnChanged(System.EventArgs.Empty);
}
}
}
}

namespace TestEvents
{
using TestCollections;

class EventListener
{
private ListWithChangedEvent m_list;

public EventListener(ListWithChangedEvent list)
{
m_list = list;

// Add "ListChanged" to the Changed event on m_list:
m_list.Changed += new ChangedEventHandler(ListChanged);
}

// This will be called whenever the list changes.
private void ListChanged(object sender, System.EventArgs e)
{
System.Console.WriteLine("This is called when the event fires.");
}

public void Detach()
{
// Detach the event and delete the list
m_list.Changed -= new ChangedEventHandler(ListChanged);
m_list = null;
}
}

class Test
{
// Test the ListWithChangedEvent class.
static void Main()
{
// Create a new list.
ListWithChangedEvent list = new ListWithChangedEvent();

// Create a class that listens to the list's change event.
EventListener listener = new EventListener(list);

// Add and remove items from the list.
list.Add("item 1");
list.Clear();
listener.Detach();
}
}
}

...全文
1014 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengmou 2007-05-26
  • 打赏
  • 举报
回复
初来此版,谢谢大家的回复.家里不能上网,结贴晚了.
amandag 2007-05-25
  • 打赏
  • 举报
回复
你雇了一个律师(注册了事件,律师就是你的委托)

当你惹上了官司(触发了事件)

你把官司的事情告诉律师(传递参数sender代表是"你"出了事,e代表你出事的信息)

律师调用他的方法解决你的问题(委托指向的方法就是事件处理程序)
ProjectDD 2007-05-25
  • 打赏
  • 举报
回复
就是委托是实例.可以写成用add{}和remove{}访问的形式
shadow841112 2007-05-25
  • 打赏
  • 举报
回复
看看VC++.net的事件说明
http://soft.yesky.com/441/2363941.shtml
ParadiseX 2007-05-25
  • 打赏
  • 举报
回复
怎么不结贴了啊。。。。要分要分
wenbin 2007-05-23
  • 打赏
  • 举报
回复
楼上的,很形象,
被打是引发事件,知道疼是接收了事件
ParadiseX 2007-05-23
  • 打赏
  • 举报
回复
打你一巴掌你总会知道疼吧。

其中你被打了就是事件,而知道疼就是你事件的回调函数

你.被打 += new EventHander("你_我被打咋办");

private void 你_我被打咋办(object sender, eventArgs e)
{
你.Say("奶奶的敢打我!");
}

其中的sender还是你自己。 要想知道谁打了你,就要从eventArgs下手了。做一个自己的eventArgs,里面加上打你的人的名字

纯粹搞笑啦:-) 别介意
wsj1983920 2007-05-23
  • 打赏
  • 举报
回复
帮顶!
hertcloud 2007-05-23
  • 打赏
  • 举报
回复
说白了 就是个 函数指针.

通过一个 代理 地址去 访问一个函数或函数队列
XMUMEEameng 2007-05-23
  • 打赏
  • 举报
回复
这个,要想打字说清楚,还真是要说半天。建议楼主看看网上很经典的C#睡前故事,仔细研究一下应该就明白了。
http://tech.itdb.cn/n/200607/17/n20060717_20635.shtml
mengmou 2007-05-23
  • 打赏
  • 举报
回复
up

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧