自定义委托,事件,参数的简单随笔
2010-03-08 17:21
465 查看
.Net Framework的编码规范:
委托类型的名称都应该以EventHandler结束。
委托的原型定义:有一个void返回值,并接受两个输入参数:一个Object 类型,一个 EventArgs类型(或继承自EventArgs)。
继承自EventArgs的类型应该以EventArgs结尾。
event本身是C#在委托基础上封装一些用于多事件注册的机制,这是event和委托的区别,委托仅仅是控件类定义和用户自定义事件方法共同遵守的方法的contract契约,一个方法类型而已 。
using System;
using System.Collections.Generic;
using System.Text;
namespace DelegateApplication
{
class Program
{
static void Main(string[] args)
{
string str = "show !!!";
InitEvent(str);
Console.ReadLine();
}
private static void InitEvent(string showStr)
{
//初始化事件参数类,赋值
InformationEventArgs eventArgs = new InformationEventArgs();
eventArgs.Information = showStr;
InformationEvent eventer = new InformationEvent();
InformationCall call = new InformationCall();
eventer.InformationClick+= new InformationHandler(call.ShowInformation);
eventer.OnInformation(eventArgs);
Console.ReadLine();
}
}
/// <summary>
/// 事件参数
/// </summary>
public class InformationEventArgs : EventArgs
{
private string _message;
public string Information
{
get
{
return _message;
}
set
{
_message = value;
}
}
}
//委托
public delegate void InformationHandler(object sender,InformationEventArgs e);
//事件
public class InformationEvent
{
public event InformationHandler InformationClick;
//触发事件
public void OnInformation(InformationEventArgs e)
{
if (InformationClick != null)
{
InformationClick(this, e);//匹配委托 进行InformationEventArgs传值
}
}
}
public class InformationCall
{
private string _strInformation;
public string InformationString
{
get
{
return _strInformation;
}
set
{
_strInformation = value;
}
}
//调用的方法 匹配委托 显示事件参数的值
public void ShowInformation(object sender, InformationEventArgs e)
{
InformationString = e.Information;
Console.WriteLine(InformationString);
}
}
}
源代码:/Files/jasenkin/DelegateApplication.rar
委托类型的名称都应该以EventHandler结束。
委托的原型定义:有一个void返回值,并接受两个输入参数:一个Object 类型,一个 EventArgs类型(或继承自EventArgs)。
继承自EventArgs的类型应该以EventArgs结尾。
event本身是C#在委托基础上封装一些用于多事件注册的机制,这是event和委托的区别,委托仅仅是控件类定义和用户自定义事件方法共同遵守的方法的contract契约,一个方法类型而已 。
using System;
using System.Collections.Generic;
using System.Text;
namespace DelegateApplication
{
class Program
{
static void Main(string[] args)
{
string str = "show !!!";
InitEvent(str);
Console.ReadLine();
}
private static void InitEvent(string showStr)
{
//初始化事件参数类,赋值
InformationEventArgs eventArgs = new InformationEventArgs();
eventArgs.Information = showStr;
InformationEvent eventer = new InformationEvent();
InformationCall call = new InformationCall();
eventer.InformationClick+= new InformationHandler(call.ShowInformation);
eventer.OnInformation(eventArgs);
Console.ReadLine();
}
}
/// <summary>
/// 事件参数
/// </summary>
public class InformationEventArgs : EventArgs
{
private string _message;
public string Information
{
get
{
return _message;
}
set
{
_message = value;
}
}
}
//委托
public delegate void InformationHandler(object sender,InformationEventArgs e);
//事件
public class InformationEvent
{
public event InformationHandler InformationClick;
//触发事件
public void OnInformation(InformationEventArgs e)
{
if (InformationClick != null)
{
InformationClick(this, e);//匹配委托 进行InformationEventArgs传值
}
}
}
public class InformationCall
{
private string _strInformation;
public string InformationString
{
get
{
return _strInformation;
}
set
{
_strInformation = value;
}
}
//调用的方法 匹配委托 显示事件参数的值
public void ShowInformation(object sender, InformationEventArgs e)
{
InformationString = e.Information;
Console.WriteLine(InformationString);
}
}
}
源代码:/Files/jasenkin/DelegateApplication.rar
相关文章推荐
- 委托与事件的简单应用(不同控件之间的控制及参数传递,C# VB)
- 委托与事件参数的简单运用
- C# 自定义参数的 事件委托
- 自定义Oracle 函数,传入参数,实现简单的功能
- [翻译]简单谈谈事件与委托
- LigerUi的Grid参数及事件简单整理
- [教程]自定义JSP中的Taglib标签之一简单入门篇(无参数的自定义标签)
- 简单事件与委托
- 简单说 JavaScript中的事件委托(下)
- [翻译]简单谈谈事件与委托 (转)
- JQuery的自定义事件代码,触发,绑定简单实例
- jquery事件委托的回调函数中调用全局变量的简单方法
- jquery事件委托的回调函数中调用全局变量的简单方法
- jquery事件委托的回调函数中调用全局变量的简单方法
- .net2005(C#)下实现事件和委托的简单示例
- 委托和简单事件的理解
- 事件 委托 简单应用
- 【PB】如何触发带参数的自定义事件?
- C# 委托事件简单示例
- 一个简单的c++/cli中委托与事件的处理模型