关于“事件”的一些回顾
有关于事件的概念呢,记得以前看过一个例子,关于猫逮老鼠的,就是说,老鼠进到一个房间里后,猫就开始逮老鼠。说老鼠不能直接告诉猫你来逮我吧,于是就通过一个监视器来通知猫,一旦猫看到老鼠就开始逮了,而这个监视器便是‘事件’。
例子:
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EventDemo
{
class Program
{
static void Main(string[] args)
{
Cat c = new Cat();//实例一只猫
c.Name = "大脸猫";
//将猫的捉鼠动作绑定到事件上
CatchMouse.CatchEvent += (o,e) => {
c.Catch();
};
Mouse m=new Mouse();//实例化一只老鼠
m.Name = "蓝皮鼠";
m.RunToHome();//进入室内
CatchMouse.DoCatch();//回调猫的捕捉事件
Console.ReadLine();
}
}
class Cat
{
public string Name { set; get; }
public void Catch()
{
Console.WriteLine("逮住你啦!我是"+Name);
}
}
class Mouse
{
public string Name { set; get; }
public void RunToHome()
{
Console.WriteLine("进来喽!我是"+Name);
}
}
public class CatchMouse
{
public static event EventHandler<CatchEventArgs> CatchEvent;//定义一个事件
public static void DoCatch()//提供外部访问事件方法
{
CatchEvent(null,null);
}
}
public class CatchEventArgs : EventArgs//事件参数类
{
public string Name { set; get; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EventDemo
{
class Program
{
static void Main(string[] args)
{
Cat c = new Cat();//实例一只猫
c.Name = "大脸猫";
//将猫的捉鼠动作绑定到事件上
CatchMouse.CatchEvent += (o,e) => {
c.Catch();
};
Mouse m=new Mouse();//实例化一只老鼠
m.Name = "蓝皮鼠";
m.RunToHome();//进入室内
CatchMouse.DoCatch();//回调猫的捕捉事件
Console.ReadLine();
}
}
class Cat
{
public string Name { set; get; }
public void Catch()
{
Console.WriteLine("逮住你啦!我是"+Name);
}
}
class Mouse
{
public string Name { set; get; }
public void RunToHome()
{
Console.WriteLine("进来喽!我是"+Name);
}
}
public class CatchMouse
{
public static event EventHandler<CatchEventArgs> CatchEvent;//定义一个事件
public static void DoCatch()//提供外部访问事件方法
{
CatchEvent(null,null);
}
}
public class CatchEventArgs : EventArgs//事件参数类
{
public string Name { set; get; }
}
}
上面这个例子呢,利用到事件参数CatchEventArgs,如果我们打算向事件中传递参数,即可利用到这个参数类。
加入上面的例子,猫只能逮住5斤一下的老鼠,那么在触发事件时就需要让猫根据事件的参数来判断该不该去逮。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EventDemo
{
class Program
{
static void Main(string[] args)
{
Cat c = new Cat();
c.Name = "大脸猫";
CatchMouse.CatchEvent += (o,e) => {
if(e.MouseWeight<5)
c.Catch();
};
double weight = 5;
Mouse m=new Mouse();
m.Name = "蓝皮鼠";
m.Weight = weight;
m.RunToHome();
CatchMouse.DoCatch(weight);
Console.ReadLine();
}
}
class Cat
{
public string Name { set; get; }
public void Catch()
{
Console.WriteLine("逮住你啦!我是"+Name);
}
}
class Mouse
{
public string Name { set; get; }
public double Weight { set; get; }
public void RunToHome()
{
Console.WriteLine("进来喽!我是"+Weight+"斤重的"+Name);
}
}
public class CatchMouse
{
public static event EventHandler<CatchEventArgs> CatchEvent;
public static void DoCatch(double mouseWeight)
{
CatchEvent(null, new CatchEventArgs() { MouseWeight=mouseWeight });
}
}
public class CatchEventArgs : EventArgs
{
public double MouseWeight { set; get; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EventDemo
{
class Program
{
static void Main(string[] args)
{
Cat c = new Cat();
c.Name = "大脸猫";
CatchMouse.CatchEvent += (o,e) => {
if(e.MouseWeight<5)
c.Catch();
};
double weight = 5;
Mouse m=new Mouse();
m.Name = "蓝皮鼠";
m.Weight = weight;
m.RunToHome();
CatchMouse.DoCatch(weight);
Console.ReadLine();
}
}
class Cat
{
public string Name { set; get; }
public void Catch()
{
Console.WriteLine("逮住你啦!我是"+Name);
}
}
class Mouse
{
public string Name { set; get; }
public double Weight { set; get; }
public void RunToHome()
{
Console.WriteLine("进来喽!我是"+Weight+"斤重的"+Name);
}
}
public class CatchMouse
{
public static event EventHandler<CatchEventArgs> CatchEvent;
public static void DoCatch(double mouseWeight)
{
CatchEvent(null, new CatchEventArgs() { MouseWeight=mouseWeight });
}
}
public class CatchEventArgs : EventArgs
{
public double MouseWeight { set; get; }
}
}
DEMO:https://files.cnblogs.com/wengyuli/EventDemo.rar