这几天在学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();
}
}
}
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();
}
}
}
http://tech.itdb.cn/n/200607/17/n20060717_20635.shtml
{
你.Say("奶奶的敢打我!");
}其中的sender还是你自己。 要想知道谁打了你,就要从eventArgs下手了。做一个自己的eventArgs,里面加上打你的人的名字纯粹搞笑啦:-) 别介意
http://soft.yesky.com/441/2363941.shtml