什么是委托?委托就是持有一个或多个方法的对象,并且该对象可以执行,可以传递。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { // 定义委托事件 delegate void ActCute(); static void Main(string[] args) { ActCute del = null; Dog dog1 = new Dog("dog"); Cat cat1 = new Cat("cat"); // 委托事件的添加 del = dog1.WagTail; del += cat1.InnocentLook; // 定义lambda 表达式 del += () => { Console.WriteLine("这是lambda表达式定义的方法!!!"); }; // 按照添加的顺序依次调用 del(); } public class Dog { private string Name; public Dog(string name) { Name = name; } public void WagTail() { Console.WriteLine(Name + "正在摇尾巴..."); } } public class Cat { private string Name; public Cat(string name) { Name = name; } public void InnocentLook() { Console.WriteLine(Name + "正在摆弄无辜表情..."); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { delegate void Func1(); // 使用方法 public static void Aminal() { Console.WriteLine("我是一只动物..."); } public static void Plant() { Console.WriteLine("我是一棵植物..."); } static void Main(string[] args) { Func1 myfunc1; myfunc1 = new Func1(Aminal); myfunc1 += new Func1(Plant); myfunc1(); } } }
委托是一个类型,事件是委托的一个特殊实例。
定义一个委托实例是不完全的,可以被外部的类调用内部的委托;事件可以在外部定义,但是触发事件是在内部。所以相对于普通的委托,事件是安全的。
事件:一种封装受限制的委托。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Client1 c1 = new Client1(); Client1 c2 = new Client1(); // 绑定委托 Dog.NewDog += c1.WangADog; Dog.NewDog += c2.WangADog; Dog dog1 = new Dog("jack"); } public class Dog { private string Name; public delegate void Handler(); // 定义委托类型 public static event Handler NewDog; // 在委托事情上定义事件 public Dog(string name) { Name = name; if (NewDog != null) { NewDog(); } } public void WagTail() { Console.WriteLine(Name + "正在摇尾巴..."); } } class Client1 { public void WangADog() { Console.WriteLine("我想要一条狗!!!"); } } } }
2019-7-29
委托和事件在窗体之间的应用:
委托:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 窗体之间的传参 { public partial class ParentForm : Form { public Action<string> ChildFrm { get; set; } // 定义一个委托 public ParentForm() { InitializeComponent(); } private void ParentForm_Load(object sender, EventArgs e) { ChildForm frm = new ChildForm(); // 初始化子窗体 ChildFrm += frm.SetText; // 绑定事件 frm.Show(); } private void Btn_Send_Click(object sender, EventArgs e) { ChildFrm(this.TB_Msg.Text); } } } ====================================================== using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 窗体之间的传参 { public partial class ChildForm : Form { public ChildForm() { InitializeComponent(); } private void ChildForm_Load(object sender, EventArgs e) { } public void SetText(string text) { this.TB_Msg.Text = text; } } }
事件:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 窗体之间的传参 { public partial class ParentForm : Form { public event EventHandler ChildFrmEvent; // 定义一个事件 public ParentForm() { InitializeComponent(); } private void ParentForm_Load(object sender, EventArgs e) { ChildForm frm = new ChildForm(); // 初始化子窗体 ChildFrmEvent += frm.FrmEvent; // 绑定子窗体里面的事件 frm.Show(); // 显示 } private void Btn_Send_Click(object sender, EventArgs e) { ChildFrmEvent(this, new NewEventArgsClass() { Text = this.TB_Msg.Text }); } } } =================================================================== using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 窗体之间的传参 { public partial class ChildForm : Form { public ChildForm() { InitializeComponent(); } private void ChildForm_Load(object sender, EventArgs e) { } public void FrmEvent(object sender, EventArgs e) { NewEventArgsClass args = e as NewEventArgsClass; TB_Msg.Text = args.Text; } } }
===================================================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks
namespace 窗体之间的传参
{
class NewEventArgsClass:EventArgs
{
public string Text { get; set; }
}
}
管家解耦父子窗体(发布订阅模式的非委托实现):
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 管家解耦父子窗体 { public partial class MasterForm : Form { public MasterForm() { InitializeComponent(); } private void MasterForm_Load(object sender, EventArgs e) { // 启动父子窗体 ParentForm PFrm = new ParentForm(); ChildForm CFrm = new ChildForm(); PFrm.ChildList = new List<TotalInterface>(); PFrm.ChildList.Add(CFrm); PFrm.Show(); CFrm.Show(); } } } ======================================================================= using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 管家解耦父子窗体 { public partial class ParentForm : Form { public List<TotalInterface> ChildList { get; set; } public ParentForm() { InitializeComponent(); } private void ParentForm_Load(object sender, EventArgs e) { } private void Btn_Send_Click(object sender, EventArgs e) { if (ChildList == null) return; foreach(TotalInterface item in ChildList) { item.SetText(this.TB_Msg.Text); } } } } ============================================================= using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 管家解耦父子窗体 { public partial class ChildForm : Form,TotalInterface { public ChildForm() { InitializeComponent(); } public void SetText(string text) { this.TB_Msg.Text = text; } private void ChildForm_Load(object sender, EventArgs e) { } } } ============================================================= using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 管家解耦父子窗体 { // 定义接口 public interface TotalInterface { void SetText(string text); } }