C# 实现自定义事件
2013-12-27 20:01
387 查看
代码中实现了三个自定义事件,分别为自定义事件、自定义事件及自定义参数、使用Action自定义事件。
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AppMain
{
/// <summary>
/// 入口
/// </summary>
public class RunEventDemo
{
#region 用Action定义事件测试
public void RunProgramThree()
{
EventDemoThree edth = new EventDemoThree();
edth.CustomerEvent += new Action<object, EventArgs>(edth_CustomerEvent);
//触发事件
edth.OnCustomerEvent();
}
void edth_CustomerEvent(object obj, EventArgs e)
{
Console.WriteLine("已调用Actiion执行的事件");
}
#endregion
#region 使用自定义事件参数事件测试
public void RunProgramTwo()
{
EventDemoTwo edt = new EventDemoTwo();
edt.CustomerEvent += new EventDemoTwo.CustomerEventHander(edt_CustomerEvent);
//创建事件参数
CustomerEventArgs cea = new CustomerEventArgs();
cea.CustomerMsg = " test";
//触发事件
edt.OnCustomerEvent(cea);
}
void edt_CustomerEvent(object sender, CustomerEventArgs e)
{
Console.WriteLine("你传入的参数值为:" + e.CustomerMsg);
}
#endregion
#region 无参的自定义事件测试
public void RunProgramOne()
{
//事件接收者
EventDemoOne ed = new EventDemoOne();
//4.注册事件处理程序
ed.customerEvent += new EventDemoOne.CustomerEventHander(RunDemo_customerEvent);
//6.调用事件
ed.OnEvent();
}
//5.编写事件处理程序
public void RunDemo_customerEvent(object sender, EventArgs e)
{
Console.WriteLine("test");
}
#endregion
}
#region无参的自定义事件
/// <summary>
/// 无参的自定义事件
/// </summary>
public class EventDemoOne
{
//1.声明关于事件的委托;
public delegate void CustomerEventHander(object sender, EventArgs e);
//2.声明事件;
public event CustomerEventHander customerEvent;
//3.编写引发事件的函数;
public void OnEvent()
{
if (this.customerEvent != null)
{
this.customerEvent(this, new EventArgs());
}
}
}
#endregion
#region自定义事件,使用自定义事件参数
/// <summary>
/// 自定义事件参数
/// </summary>
public class CustomerEventArgs : EventArgs
{
public string CustomerMsg { get; set; }
}
public class EventDemoTwo
{
public delegate void CustomerEventHander(object sender, CustomerEventArgs e);
public event CustomerEventHander CustomerEvent;
//3.编写引发事件的函数,注意多了个参数;
public void OnCustomerEvent(CustomerEventArgs e)
{
this.CustomerEvent(this, e);
}
}
#endregion
#region使用Action自定义事件
public class EventDemoThree
{
public event Action<object, EventArgs> CustomerEvent;
public void OnCustomerEvent()
{
this.CustomerEvent(this, new EventArgs());
}
}
#endregion
}
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AppMain
{
/// <summary>
/// 入口
/// </summary>
public class RunEventDemo
{
#region 用Action定义事件测试
public void RunProgramThree()
{
EventDemoThree edth = new EventDemoThree();
edth.CustomerEvent += new Action<object, EventArgs>(edth_CustomerEvent);
//触发事件
edth.OnCustomerEvent();
}
void edth_CustomerEvent(object obj, EventArgs e)
{
Console.WriteLine("已调用Actiion执行的事件");
}
#endregion
#region 使用自定义事件参数事件测试
public void RunProgramTwo()
{
EventDemoTwo edt = new EventDemoTwo();
edt.CustomerEvent += new EventDemoTwo.CustomerEventHander(edt_CustomerEvent);
//创建事件参数
CustomerEventArgs cea = new CustomerEventArgs();
cea.CustomerMsg = " test";
//触发事件
edt.OnCustomerEvent(cea);
}
void edt_CustomerEvent(object sender, CustomerEventArgs e)
{
Console.WriteLine("你传入的参数值为:" + e.CustomerMsg);
}
#endregion
#region 无参的自定义事件测试
public void RunProgramOne()
{
//事件接收者
EventDemoOne ed = new EventDemoOne();
//4.注册事件处理程序
ed.customerEvent += new EventDemoOne.CustomerEventHander(RunDemo_customerEvent);
//6.调用事件
ed.OnEvent();
}
//5.编写事件处理程序
public void RunDemo_customerEvent(object sender, EventArgs e)
{
Console.WriteLine("test");
}
#endregion
}
#region无参的自定义事件
/// <summary>
/// 无参的自定义事件
/// </summary>
public class EventDemoOne
{
//1.声明关于事件的委托;
public delegate void CustomerEventHander(object sender, EventArgs e);
//2.声明事件;
public event CustomerEventHander customerEvent;
//3.编写引发事件的函数;
public void OnEvent()
{
if (this.customerEvent != null)
{
this.customerEvent(this, new EventArgs());
}
}
}
#endregion
#region自定义事件,使用自定义事件参数
/// <summary>
/// 自定义事件参数
/// </summary>
public class CustomerEventArgs : EventArgs
{
public string CustomerMsg { get; set; }
}
public class EventDemoTwo
{
public delegate void CustomerEventHander(object sender, CustomerEventArgs e);
public event CustomerEventHander CustomerEvent;
//3.编写引发事件的函数,注意多了个参数;
public void OnCustomerEvent(CustomerEventArgs e)
{
this.CustomerEvent(this, e);
}
}
#endregion
#region使用Action自定义事件
public class EventDemoThree
{
public event Action<object, EventArgs> CustomerEvent;
public void OnCustomerEvent()
{
this.CustomerEvent(this, new EventArgs());
}
}
#endregion
}
相关文章推荐
- 在C#使用字典存储事件示例及实现自定义事件访问器
- C#实现自定义事件,用于监视变量变化
- asp.net中c#自定义事件的实现方法详解
- 通过c# 实现自定义属性改变触发自定义事件 ,理解自定义事件及其触发过程
- C#:自定义消息事件的实现
- c# 与java 相同实现 自定义事件的触发及监听
- C#实现自定义双击事件
- 在C#中实现自定义事件
- C#实现Winform自定义半透明遮罩层
- C#实现自定义定时组件的方法
- js实现屏蔽默认快捷键调用自定义事件示例
- RichTextBox实现关键字自定义颜色显示(C#)
- Android 自定义view模板并实现点击事件的回调
- 解决C#中WebBrowser的DocumentCompleted事件不执行的实现方法
- C#读取配置文件详解 完全自定义实现动态加载dll
- 自定义cell创建 button, 在控制器实现点击事件
- 提取aspnet_Profile中自定义的用户属性(C#实现)
- C#自定义事件及用法实例
- C#中使用IFormattable实现自定义格式化字符串输出示例