//目的:利用匿名委托和动态事件注册机制实现应用程序状态监视
//限制:控制事件均通过CommandHelper注册
// 事件注册帮助类
public class CommandHelper
{
public static void PerformHandler(object sender, EventArgs e)
{
//在此方法中实现对于用户是否操作的状态捕捉;
}
/// <summary>
/// 注册事件
/// </summary>
/// <param name="command">命令对象</param>
/// <param name="handler">响应委托</param>
public static void RegisteClickEvent(object command, System.EventHandler handler)
{
System.Reflection.EventInfo eventInfo = command.GetType().GetEvent("Click", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
if (eventInfo != null)
{
eventInfo.AddEventHandler(command, handler);
eventInfo.AddEventHandler(command, new System.EventHandler(PerformHandler));
}
}
}
//客户代码
//注册按键事件
.....
CommandHelper.RegisteClickEvent(this.tsbAddNew,delegate(object o,EventArgs p)
{
//点击时调用子窗体
QxFunctionManager_Edit editor = new QxFunctionManager_Edit();
editor.ShowDialog();
}
);
......