事件(Event)简介
类型的成员,使对象或类具备通知能力的成员
对象A拥有一个事件B,当事件B发生时,对象A又能通知别人的能力,且会推送通知内容(事件参数)
根据通知和事件参数来采取行动的行为称为“响应事件”,响应事件时所做的事称为“事件处理器(Event Handler)
事件的功能:通知 + 可选的事件参数
事件的用法:用于对象或类之间的动作协调与信息传递
事件模型(Event model):也就是“发生-响应”模型。“发生-响应”中有五个部分:对象A存在事件B;另一个或一群对象“订阅”了这个事件B;事件B发生;“订阅”了这个事件的对象依次被通知;被通知到的对象根据事件参数来对事件进行响应
事件组成
- 事件的拥有者(event source,对象)
- 事件成员(event,成员)
- 事件的响应者(event subscriber,对象)
- 事件处理器(event handler,方法成员):一种回调方法
- 事件订阅:将事件处理器与事件关联在一起
事件不会主动发生,在被拥有者的某些内部逻辑触发后才会发生
用于订阅事件的事件处理器必须和事件遵守同一个约定,约定可以约束事件发送给事件处理器的消息的类型,还约束了事件处理器能够处理消息的类型。这个约定就是委托
事件应用
using System;
using System.Timers;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Timer timer = new Timer();//timer就是事件的拥有者
timer.Interval = 1000;//设置间隔,单位为毫秒
Kid kid = new Kid();//kid是事件响应者
timer.Elapsed += kid.Action;//鼠标移到Action下的红色波浪线再Ctrl+.自动生成方法
//+=:订阅事件
//Elapsed事件:达到间隔时发生
//成员前有“闪电”就是事件;“方块”是方法;“扳手”是属性
timer.Start();//开始计数
Console.ReadLine();
}
}
class Kid
{ //先不写Kid中的内容,在主函数中写到订阅事件后,Ctrl+.直接生成Action方法
internal void Action(object sender, ElapsedEventArgs e)
//sender是事件拥有者
{
Console.WriteLine("One Second");
}
}
}
sender参数:事件拥有者,当多个事件发起者指向同一个事件时,在事件中可以用 if 来判断是哪个事件发起者
Kid.Action使用Ctrl+. 后自动生成如下方法:
internal void Action(object sender, ElapsedEventArgs e)
{
throw new NotImplementedException();
}
事件拥有者与事件响应者为两个不同的对象
using System;
using System.Windows.Forms;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Form form = new Form();//form 就是事件拥有者
Controller controller = new Controller(form);
form.ShowDialog();
}
}
class Controller
{
private Form form;//字段
public Controller(Form form)//一个构造器
{
if (form != null)
{
this.form/*字段*/ = form;//传进来的参数
this.form.Click += this/*this代表Controller的实例*/.FormClicked;
}
} //一样使用Ctrl+. 生成方法
private void FormClicked(object sender, EventArgs e)
{
this.form.Text = DateTime.Now.ToString();
}
}
}
事件的拥有者与事件的响应者是同一个对象
using System;
using System.Windows.Forms;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
MyForm form = new MyForm();//form 就是事件拥有者
form.Click += form.FormClicked;
form.ShowDialog();
}
}
class MyForm : Form//Form是已经准备好的,无法修改,不扩展就无法Ctrl+.生成方法
{ //继承Form
internal void FormClicked(object sender, EventArgs e)
{
this.Text = DateTime.Now.ToString();
}
}
}
事件拥有者是事件响应者的一个字段成员
using System;
using System.Drawing;//用来更改button的大小
using System.Windows.Forms;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
MyForm form = new MyForm();
form.ShowDialog();
}
}
class MyForm : Form
{
private TextBox textBox;//字段1,是准备好了的,不能添加事件处理器
private Button button;//字段2,是准备好了的,不能添加事件处理器
//button是事件拥有者
public MyForm()
{
this.textBox = new TextBox();
this.button = new Button();
this.button.Size = new Size/*using System.Drawing;*/(110, 50);//更改button大小
this.button.Location = new Point(50, 60);//更改button横纵坐标
this.Controls.Add(textBox);//将字段1显示在窗口上
this.Controls.Add(button);//将字段2显示在窗口上
this.button.Click += this.ButtonClicked;//Click是事件
this.button.Text = "Click me";
}
private void ButtonClicked(object sender, EventArgs e)
{ //ButtonClicked是事件处理器
this.textBox.Text = "Hello World";
} //事件响应者是MyForm的对象
}
}