博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C Sharp (c#) - Events – fire that event
阅读量:6975 次
发布时间:2019-06-27

本文共 7851 字,大约阅读时间需要 26 分钟。

Click  to contact me if you require any freelance development
 

Events – fire that event

An event will be fired when something has happened and then you can link to that event and do something else if you wanted to. To start of with you have to link a event with a  function which will be the event handler.

To setup a delegate you can have the object and also any event arguments ( you can add in more if you want to, but these are the basics to include)

// setup a delegate for the event to fire with.	public delegate void ChangedEventHandler(object sender, EventArgs e);

here is the class that will actual fire the event, to start with I link the “event” to a virtual method as such called Changed (with the delegate from above ChangedEventHander).

// this is the MyEvent class that will create the event fired.	public class MyEvent 	{
private int x;  // the event for this internal class public event ChangedEventHandler Changed;

here is the event firing function, this will call the Changed “virtual” method as such when ever you call this function.

protected virtual void ThisHasChanged(EventArgs e)		{
// try and call the delegate ChangedEventHandler // make sure that there is something to call for the event to fire. if (Changed != null) Changed(this, e); }

and here is the get/set for the internal X variable and in the set I call the ThisHasChanged function above.

// call the ThisHasChanged event when you set the internal value of X.		public int xValue		{
get {
return x;} set {
x = value;ThisHasChanged(EventArgs.Empty); } } }

So that is the event firing, but you will need to have a listener that will listen to any events and call something that will deal with the fired event, so here is the listener class.

So the listener, is just like any other class, but you will need to have a internal reference to the class above so that you can link to the event fired, so here is the MyEvent internal class for the listener.

// the listener for the event firing	class EventListener	{
// MyEvent class private MyEvent ThisIsMyEvent;

and then the constructor for the EventListener, you pass in the MyEvent class that you want to “link” (the one that you will create within the running part of the program). and from within the MyEvent, there was a “virtual” method as such called “Changed”, well now we can create a link so that when that event “Changed” has been called from within the MyEvent Class I can link to a function within this class EventListener with using a new delegate ChangedEventHandler and the ValueHadChanged is within the EventListener class.

// the class constructor and you need to pass the MyEvent class to link to the listener.		public EventListener(MyEvent SetUpTheEventLink)		{
// Create a link to the MyEvent class passed in to the internal MyEvent Class ThisIsMyEvent = SetUpTheEventLink; // this is the link to the listern function. ThisIsMyEvent.Changed += new ChangedEventHandler(ValueHadChange); }

here is the ValueHadChange, same setup as the delegate parameters since we are basically passing event details between them both and can pass in more details is you want to, but you really only want to have the object (the MyEvent class) and any event arguments e.g. errors etc. at present it just says , “Oh yeah.. I have altered”.

// same style as the delegate e.g. object, eventargs, that is sent from the event fired.		// THIS IS THE LISTERN as such, function..if you want to do something like.. do not alter the value!! 		private void ValueHadChange(object sender, EventArgs e)		{
Console.WriteLine("Oh yeah.. I have been altered"); }

since there was a Changed link attached to the above function, to clean up you can detach the link with using something similar to the constructor of this class, but instead of using += (to add) you just need to subtract from the Change virtual method -=

// detach the listener function from the event MyEvent 		public void Detach()		{
// take the event linked from the MyEvent class. ThisIsMyEvent.Changed -= new ChangedEventHandler(ValueHadChange); ThisIsMyEvent = null; } }

Here is the full code

using System; // when alter a number in the internal part of the class.namespace codingfriendsEvents{
// setup a delegate for the event to fire with. public delegate void ChangedEventHandler(object sender, EventArgs e);  // this is the MyEvent class that will create the event fired. public class MyEvent {
// the event for this internal class public event ChangedEventHandler Changed;  private int x;  protected virtual void ThisHasChanged(EventArgs e) {
// try and call the delegate ChangedEventHandler // make sure that there is something to call for the event to fire. if (Changed != null) Changed(this, e); }  // call the ThisHasChanged event when you set the internal value of X. public int xValue {
get {
return x;} set {
x = value;ThisHasChanged(EventArgs.Empty); } } }  // the listener for the event firing class EventListener {
// MyEvent class private MyEvent ThisIsMyEvent;  // same style as the delegate e.g. object, eventargs, that is sent from the event fired. // THIS IS THE LISTERN as such, function..if you want to do something like.. do not alter the value!! private void ValueHadChange(object sender, EventArgs e) {
Console.WriteLine("Oh yeah.. I have been altered"); }  // the class constructor and you need to pass the MyEvent class to link to the listener. public EventListener(MyEvent SetUpTheEventLink) {
// Create a link to the MyEvent class passed in to the internal MyEvent Class ThisIsMyEvent = SetUpTheEventLink; // this is the link to the listern function. ThisIsMyEvent.Changed += new ChangedEventHandler(ValueHadChange); }  // detach the listener function from the event MyEvent public void Detach() {
// take the event linked from the MyEvent class. ThisIsMyEvent.Changed -= new ChangedEventHandler(ValueHadChange); ThisIsMyEvent = null; } }  class TestMyEvent {
public static void Main() {
// create a new MyEvent class MyEvent MyEventToCheck = new MyEvent();  // link in with the EventLister , passing in the class of MyEvent EventListener TheListener = new EventListener(MyEventToCheck);  // should fire of a event !! because I am changing the internal x value. MyEventToCheck.xValue = 10;  // will not fire a event because I have not changed it. Console.WriteLine("X = " + MyEventToCheck.xValue);  // will fire of another event. MyEventToCheck.xValue = 5;  TheListener.Detach(); } }}

and here would be the output

Oh yeah.. I have been alteredX = 10Oh yeah.. I have been altered

If you want to gain access to the object sender within the EventListener class to the function that was listening to the event fired, if you alter it as below, but also since because the MyEvent was also linked within the constructor you also have that link as well.

// same style as the delegate e.g. object, eventargs, that is sent from the event fired.		// THIS IS THE LISTERN as such, function..if you want to do something like.. do not alter the value!! 		private void ValueHadChange(object sender, EventArgs e)		{
Console.WriteLine("Oh yeah.. I have been altered"); Console.WriteLine("New value of X = " + ((MyEvent)sender).xValue); Console.WriteLine("Internal MyEvent class x = " + ThisIsMyEvent.xValue); }

then you can gain access to the MyEvent from within the object sender and the output would be

Oh yeah.. I have been alteredNew value of X = 10Internal MyEvent class x = 10X = 10Oh yeah.. I have been alteredNew value of X = 5Internal MyEvent class x = 5

转载地址:http://clesl.baihongyu.com/

你可能感兴趣的文章
vim与外部文件的粘帖复制
查看>>
Entity Framework DBFirst尝试
查看>>
pojBuy Tickets2828线段树或者树状数组(队列中倒序插队)
查看>>
【BZOJ】1600: [Usaco2008 Oct]建造栅栏(dp)
查看>>
linux下启动oracle
查看>>
【原创】开源Math.NET基础数学类库使用(02)矩阵向量计算
查看>>
SqlHelper
查看>>
前端画面-下拉后滚动
查看>>
golang使用http client发起get和post请求示例
查看>>
remoting生命周期
查看>>
[zz]malloc()和calloc()
查看>>
Sylius – 100% 免费和开源的电子商务解决方案
查看>>
单片机系列学习
查看>>
BZOJ3571 : [Hnoi2014]画框
查看>>
读枯燥的技术书时怎么集中精神?
查看>>
【github】github 使用教程初级版
查看>>
iOS 依据文本内容为TextView动态定义高度
查看>>
信号练习
查看>>
UML类图几种关系的总结
查看>>
JavaScript学习笔记——流程控制
查看>>