一、懂得C#中的预定义事务处理惩罚机制
在写代码前我们先来熟悉.net框架中和事务有关的类和委托,懂得C#中预定义事务的处理惩罚。
EventArgs是包含事务数据的类的基类,用于传递事务的细节。
EventHandler是一个委托声明如下
public delegate void EventHandler( object sender , EventArgs e )
重视这里的参数,前者是一个对象(其实这里传递的是对象的引用,若是是button1的click事务则sender就是button1),后面是包含事务数据的类的基类。
下面我们研究一下Button类看看此中的事务声明(应用WinCV对象查看),以Click事务为例。
public event EventHandler Click;
这里定义了一个EventHandler类型的事务Click
前面的内容都是C#在类库中已经为我们定义好了的。下面我们来看编程时产生的代码。
private void button1_Click(object sender, System.EventArgs e)
{
...
}
这是我们和button1_click事务所对应的办法。重视办法的参数合适委托中的签名(既参数列表)。那我们怎么把这个办法和事务接洽起来呢,请看下面的代码。
this.button1.Click += new System.EventHandler(this.button1_Click);
把this.button1_Click办法绑定到this.button1.Click事务。
下面我们研究一下C#事务处理惩罚的工作流程,起首体系会在为我们创建一个在后台监听事务的对象(若是是 button1的事务那么监听事务的就是button1),这个对象用来产闹事务,若是有某个用户事务产生则产生对应的应用法度事务,然后履行订阅了事务 的所有办法。
二、简单的自定义事务(1)
首先我们须要定义一个类来监听客户端事务,这里我们监听键盘的输入。
定义一个委托。
public delegate void UserRequest(object sender,EventArgs e);
前面的object用来传递事务的产生者,后面的EventArgs用来传递事务的细节,如今临时没什么用处,一会后面的例子中将应用。
下面定义一个此委托类型类型的事务
public event UserRequest OnUserRequest;
下面我们来做一个死轮回
2 {
3 bool finished=false;
4 do
5 {
6 if (Console.ReadLine()=="h")
7 {
8 OnUserRequest(this,new EventArgs());
9 }
10 }while(!finished);
11 }
此代码连续的请求用户输入字符,若是输入的成果是h,则触发OnUserRequest事务,事务的触发者是本身(this),事务细节无(没有传递任何参数的EventArgs实例)。我们给这个类取名为UserInputMonitor。
下面我们要做的是定义客户端的类
起首得实例化UserInputMonitor类
UserInputMonitor monitor=new UserInputMonitor();
然后我们定义一个办法。
private void ShowMessage(object sender,EventArgs e)
{
Console.WriteLine("HaHa!!");
}
最后要做的是把这个办法和事务接洽起来(订阅事务),我们把它写到库户端类的机关函数里。
Client(UserInputMonitor m)
{
m.OnUserRequest+=new UserInputMonitor.UserRequest(this.ShowMessage);
//注意:这种写法是错误的,因为委托是静态的
//m.OnUserRequest+=new m.UserRequest(this.ShowMessage);
}
下面创建客户端的实例。
new Client(monitor);
对了,别忘了让monitor开端监听事务。
monitor.run();
大功成功,代码如下:
2 class UserInputMonitor
3 {
4public delegate void UserRequest(object sender,EventArgs e);
5//定义委托
6public event UserRequest OnUserRequest;
7//此委托类型类型的事务
8public void Run()
9{
10bool finished=false;
11do
12{
13if (Console.ReadLine()=="h")
14{
15OnUserRequest(this,new EventArgs());
16}
17}while(!finished);
18}
19}
20
21 public class Client
22 {
23public static void Main()
24{
25UserInputMonitor monitor=new UserInputMonitor();
26new Client(monitor);
27monitor.Run();
28}
29private void ShowMessage(object sender,EventArgs e)
30{
31Console.WriteLine("HaHa!!");
32}
33Client(UserInputMonitor m)
34{
35m.OnUserRequest+=new UserInputMonitor.UserRequest(this.ShowMessage);
36//m.OnUserRequest+=new m.UserRequest(this.ShowMessage);
37//重视这种写法是错误的,因为委托是静态的
38}
39}
40
三、进一步研究C#中的预定义事务处理惩罚机制
可能大师发明在C#中有些事务和前面的似乎不太一样。例如
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
}
this.textBox1.KeyPress+=newSystem.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
这里应用了KeyPressEventArgs而不是EventArgs作为参数。这里应用了KeyEventHandler委托,而不是EventHandler委托。
KeyPressEventArgs是EventArgs的派生类,而KeyEventHandler的声明如下
public delegate void KeyEventHandler( object sender , KeyEventArgs e );
是参数为KeyEventArgs的委托。那为什么KeyPress事务要这么做呢,我们可以从两个类的机关函数来找答案。
public EventArgs();
public KeyPressEventArgs(char keyChar);
这里的keyData是什么,是用来传递我们按下了哪个键的,哈。
我在KeyEventArgs中又发了然属性
public char KeyChar { get; }
进一步证了然我的理论。下面我们来做一个类似的例子来帮助懂得。
四、简单的自定义事务(2)
拿我们上方做的例子来改。
我们也定义一个EventArgs(类似KeyEventArgs)取名MyEventArgs,定义一个机关函数public MyEventArgs(char keyChar),同样我们也设置响应的属性。代码如下
2 class MyMyEventArgs:EventArgs
3 {
4private char keyChar;
5public MyMyEventArgs(char keyChar)
6{
7this.keychar=keychar;
8}
9public char KeyChar
10{
11get
12{
13return keyChar;
14}
15}
16}
17
18
因为如今要监听多个键了,我们得改写****的类中的do...while项目组。改写委托,改写客户端传递的参数。好了终极代码如下,好累
2 class MyEventArgs:EventArgs
3 {
4private char keyChar;
5public MyEventArgs(char keyChar)
6{
7this.keyChar=keyChar;
8}
9public char KeyChar
10{
11get
12{
13return keyChar;
14}
15}
16}
17
18 class UserInputMonitor
19 {
20public delegate void UserRequest(object sender,MyEventArgs e);
21//定义委托
22public event UserRequest OnUserRequest;
23//此委托类型类型的事务
24public void Run()
25{
26bool finished=false;
27do
28{
29string inputString= Console.ReadLine();
30if (inputString!="")
31OnUserRequest(this,new MyEventArgs(inputString[0]));
32}while(!finished);
33}
34}
35
36
37 public class Client
38 {
39public static void Main()
40{
41UserInputMonitor monitor=new UserInputMonitor();
42new Client(monitor);
43monitor.Run();
44}
45private void ShowMessage(object sender,MyEventArgs e)
46{
47Console.WriteLine("捕获到:{0}",e.KeyChar);
48}
49Client(UserInputMonitor m)
50{
51m.OnUserRequest+=new UserInputMonitor.UserRequest(this.ShowMessage);
52//m.OnUserRequest+=new m.UserRequest(this.ShowMessage);
53//重视这种写法是错误的,因为委托是静态的
54}
55}
56