【22】C# 委托

看到项目有使用委托,一直都搞不明白是怎么回事,看了好几遍才略懂一二,关于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();
        }
    }
}

3956112-c8a64e4a5a4ac82f.png
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);
        }
    }
}

3956112-92cf5f8f0006ef46.png
2018-03-24_013807.png
参考文章

委托(C# 编程指南)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值