c# 委托机制
public class AppEvent{
//键值对委托的形式
public static AppEvent Events = new AppEvents()
private Dictionary<string, EventHandler> DelegateList = new Dictionary<string ,EventHandler>();
#region 装备事件索引
public const string Aoi_GeoDraw="aoigeodraw";
#endregion
public void AddListener(string name,EventHandler handler)
{
if(!DelegateList.ContainsKey(name))
{
DelegateList.Add(name,handler);
}
else
{
DelegateList.Remove(name);
DelegateList.Add(name,handler);
}
}
//执行方法
public void Dispath(string name,object obj)
{
if(DelegateList.ContainsKey(name))
{
DelegateList[name](obj,null);
}
}
}
// 窗体调用
// 初始化监听对象
private vid InitalizeEventListener(string list_AoiValue)
{
AppEvent.Events.Dispatch(AppEvent.Aoi_GeoDraw,list_AoiValue);
}
//事件监听
private void InitializeEventListener()
{
AppEvent.Events.AddListener(AppEvent.Aoi_GeoDraw,Aoi_GeoDrawEvent);
}
// 触发事件
private void Aoi_GeoDrawEvent(object sender,EventArgs e)
{
if(sender!=null)
{
txt_Draw=sender.ToString()
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步