看到项目有使用委托, 一直都搞不明白是怎么回事, 看了好几遍才略懂一二, 关于 c# 接触时间时间短, 目前工作有用到 c# 进行开发, 实际工作中写的更多的是业务代码, 一些技巧性的东西, 还是得下去找时间研究一下, 不然还是一知半解, 不知所云
简介
委托类似与 C/C++ 中的指针, 它是一种引用类型, 表示对具有特定参数列表和返回类型的方法的引用 在实例化委托时, 你可以将其实例与任何具有兼容签名和返回类型的方法相关联 你可以通过委托实例调用方法使用 delegate 进行声明
例子
- // 声明一个委托, 有一个参数, 且无返回值的函数, 都可以使用委托来调用
- public delegate void DelegateHandel(string message);
- public static void DelegateMethod(string message)
- {
- System.Console.WriteLine(message);
- }
- // 实例化委托
- DelegateHandel delhandel=DelegateMethod;
- // 调用委托
- delhandel("hello world");
多播委托
你可以使用 + 来将多个对象关联到一个委托实例上, 使用 - 将其取消关联
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace testdelegate
- {
- delegate void delhandel(string s);
- class Program
- {
- static void hello(string s)
- {
- System.Console.WriteLine("hello{0}",s);
- }
- static void world(string s)
- {
- System.Console.WriteLine("world{0}",s);
- }
- static void Main(string[] args)
- {
- delhandel del1, del2, del3,del4;
- del1 = hello;
- del2 = world;
- del3 = hello;
- del3 += world;
- del4 = del3 - del2;
- del1("A");
- del2("B");
- del3("C");
- del4("D");
- System.Console.ReadLine();
- }
- }
- }
2018-03-24_010258.png
利用委托进行窗口传消息
先创建一个主窗口和一个子窗口, 在主窗口中添加一个按钮用来显示出子窗口, 在子窗口中添加一个按钮用来传递消息给主窗口子窗口的按钮这里我们用它来改变主窗口的背景颜色, 你可以传递文字消息
- //childwindow.xaml.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- namespace testdel
- {
- /// <summary>
- /// childwindow.xaml 的交互逻辑
- /// </summary>
- public partial class childwindow : Window
- {
- // 定义一个委托
- public delegate void ChangeHandel();
- // 定义委托的事件
- public event ChangeHandel ChangeEvent;
- public childwindow()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- // 判断这个事件是否有注册
- if (ChangeEvent != null)
- {
- ChangeEvent();
- }
- }
- }
- }
- //mainwindow.xaml.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace testdel
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- childwindow childwin = new childwindow();
- // 显示子窗口时添加事件订阅
- childwin.ChangeEvent += new childwindow.ChangeHandel(changecolor);
- childwin.Show();
- }
- private void changecolor()
- {
- backgrid.Background = new SolidColorBrush(Colors.Red);
- }
- }
- }
2018-03-24_013807.png
参考文章
委托 (C# 编程指南)
来源: http://www.jianshu.com/p/101cb2dbd8c8