黄石百度做网站多少钱,做护肤的网站有哪些,做ui的网站,产品宣传网站的作用C# 事件 一、事件二、事件的应用三、事件的自定义声明 一、事件
定义#xff1a;“a thing that happens, especially something important” / “能够发生的什么事情”角色#xff1a;使对象或类具备通知能力的成员使用#xff1a;用于对象或类间的动作协调与信息传递事件… C# 事件 一、事件二、事件的应用三、事件的自定义声明 一、事件
定义“a thing that happens, especially something important” / “能够发生的什么事情”角色使对象或类具备通知能力的成员使用用于对象或类间的动作协调与信息传递事件的功能通知可选的事件参数原理事件模型中的两个“5” “发生-响应”中的5个部分事件的拥有者事件的响应者事件事件处理器事件订阅“发生-响应”中的5个动作 1我有一个事件 2一个人或者一群人关心即“订阅”我这个事件 3我的这个事件发生了 4关心这个事件的人会被依次通知到 5被通知的人根据拿到的信息即“事件数据”、“事件参数”、“通知”对事件进行响应即“事件处理” 事件的本质是委托字段的一个包装器。
二、事件的应用
事件模型的五个组成部分
事件的拥有者event source对象事件event成员事件的响应者event subscriber对象事件处理器event handler成员——本质上是一个回调方法事件订阅——把事件处理器和事件关联在一起本质上是一种以委托类型为基础的“约定”
using System.Timers;
namespace EventExample
{class Program{static void Main(string[] args){System.Timers.Timer timer new System.Timers.Timer();//1.事件的拥有者timer.Interval 1000;Boy boy new Boy();//3.事件的响应者//2.事件Elapsedtimer.Elapsed new ElapsedEventHandler( boy.Action);//5.订阅事件timer.Start();Console.ReadLine(); }}class Boy{//4.事件处理器internal void Action(object? sender, ElapsedEventArgs e){Console.WriteLine(Jump);}}}第一种组合模式事件的拥有者和事件的响应者是不同的对象
namespace EventExample2
{internal static class Program{static void Main(){Form form new 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.FormClicked;}}private void FormClicked(object sender, EventArgs e){this.form.Text DateTime.Now.ToString();}}
}
第二种组合模式事件的拥有者和时间的响应者是同一个对象
namespace EventExample3
{internal static class Program{static void Main(){MyForm form new MyForm();form.Click form.FormClicked;form.ShowDialog();}}class MyForm : Form{internal void FormClicked(object sender, EventArgs e){this.Text DateTime.Now.ToString();}}}第三张组合模式事件的拥有者是事件的响应者的一个字段成员
namespace EventExample4
{internal static class Program{static void Main(){MyForm form new MyForm(); form.ShowDialog();}}class MyForm : Form{private TextBox textBox;private Button button;public MyForm(){this.textBox new TextBox();this.button new Button();this.button.Text Say Hello!;this.button.Top 30;this.button.Click this.ButtonClicked;this.Controls.Add(this.textBox);this.Controls.Add(this.button);}private void ButtonClicked(object sender, EventArgs e){this.textBox.Text Hello world!;}}}订阅的几种写法 //方法一直接挂载处理器this.button1.Click Button1_Click;//方法二使用委托this.button1.Click new EventHandler(this.Button1_Click);//方法三使用委托this.button1.Click delegate (object? sender, EventArgs e){this.Text DateTime.Now.ToString();};//简写this.button1.Click (sender, e){this.Text DateTime.Now.ToString();};三、事件的自定义声明
完整声明
规定输出类型和输入参数根据规定声明委托在事件拥有者类中声明委托字段根据委托声明事件订阅事件把委托和响应事件绑定调用委托 (相对于间接调用了响应事件)
namespace EventExample
{class Program{public static void Main(string[] args){Customer customer new Customer();Waiter waiter new Waiter();customer.Order waiter.Action;customer.Action();customer.PayTheBill();}}public class OrderEventArgs : EventArgs{public string DishName { get; set; }public string Size { get; set; }}//声明委托public delegate void OrderEventHandler(Customer customer, OrderEventArgs e);public class Customer{//声明委托字段private OrderEventHandler? orderEventHandlerOrder;//声明事件public event OrderEventHandler Order{add{this.orderEventHandlerOrder value;}remove{this.orderEventHandlerOrder - value;}}public double Bill { get; set; }public void PayTheBill(){Console.WriteLine(I will pay ${0}., this.Bill);}public void WalkIn(){Console.WriteLine(Walk into the restaurant.);}public void SitDown(){Console.WriteLine(Sit down.);}public void Think(){for (int i 0; i 5; i){Console.WriteLine(Let me think...);Thread.Sleep(1000);}if (this.orderEventHandlerOrder ! null){OrderEventArgs e new OrderEventArgs();e.DishName 辣椒擂皮蛋;e.Size large;this.orderEventHandlerOrder.Invoke(this, e);}}public void Action(){Console.ReadLine();this.WalkIn();this.SitDown();this.Think();}}public class Waiter{internal void Action(Customer customer, OrderEventArgs e){Console.WriteLine(I will serve you the dish -{0}., e.DishName);double price 10;switch (e.Size){case small:price price * 0.5;break;case large:price price * 1.5;break;}customer.Bill price;}}} 简略声明 public class Customer{//简略声明事件public event OrderEventHandler Order;....public void Think(){...if (this.Order ! null){...this.Order.Invoke(this, e);}}public void Action(){...this.Think();}}使用EventHandler声明事件 public delegate void EventHandler(object? sender, EventArgs e); public class Customer{//使用EventHandler 声明事件public event EventHandler Order;...}public class Waiter{internal void Action(Object sender, EventArgs e){//强制转换Customer? customer sender as Customer;OrderEventArgs? orderInfo e as OrderEventArgs;...}}注意
一般事件命名为××EventHandler事件参数命名为××EventArgs触发事件命名为On××访问级别为protected 修改后的代码 public class Customer{...this.OnOrder(辣椒擂皮蛋, large); }private void OnOrder(string dishName, string size) {if (this.Order ! null){OrderEventArgs e new OrderEventArgs();e.DishName dishName;e.Size size;this.Order.Invoke(this, e);}}...}