第十四章 事件

2019-03-22  本文已影响0人  向着远方奔跑

14.1 发布者/订阅者

有关事件的私有委托

14.2 源代码组件概览

一个完整的组件由5部分组成:

static class Program
    {
        static void Main()
        {
            Incrementer incrementer = new Incrementer();
            Dozens dozensCounter = new Dozens(incrementer);
            incrementer.DoCount();
            Console.WriteLine("Number of dozens = {0}", dozensCounter.DozensCount);
            Console.ReadKey();
        }
//发布者类
        class Incrementer
        {
            public delegate void Handler();//委托类型声明
            public event Handler CountADozen;//事件声明

            public void DoCount()
            {
                for (int i = 1; i < 100; i++)
                {
                    if (i % 12 == 0 && CountADozen != null)
                    {
                        CountADozen();//触发事件代码
                    }
                }
            }
        }
//订阅者类
        class Dozens
        {
            public int DozensCount { get; private set; }

            public Dozens(Incrementer incrementer)
            {
                DozensCount = 0;
                incrementer.CountADozen += IncrementerDozensCount;//事件注册
            }

            void IncrementerDozensCount()//事件处理程序声明
            {
                DozensCount++;
            }
        }
    }

14.6 标准事件的用法

程序事件的异步处理是使用C#事件的绝佳场景。WindowsGUI编程广泛地使用了事件,.NET框架提供了一个标准模式。事件使用的标准模式的根本是System命名空间声明的EventHandler委托类型。
public delegate void EventHandler(object sender, EventArgs e);

修改上述程序使之使用标准EventHandler委托:

static class Program
    {
        static void Main()
        {
            Incrementer incrementer = new Incrementer();
            Dozens dozensCounter = new Dozens(incrementer);
            incrementer.DoCount();
            Console.WriteLine("Number of dozens = {0}", dozensCounter.DozensCount);
            Console.ReadKey();
        }

        class Incrementer
        {
            public event EventHandler CountADozen;//使用系统定义的EventHandler委托

            public void DoCount()
            {
                for (int i = 1; i < 100; i++)
                {
                    if (i % 12 == 0 && CountADozen != null)
                    {
                        CountADozen(this, null);//触发事件时使用EventHandler的参数
                    }
                }
            }
        }

        class Dozens
        {
            public int DozensCount { get; private set; }

            public Dozens(Incrementer incrementer)
            {
                DozensCount = 0;
                incrementer.CountADozen += IncrementerDozensCount;
            }

            void IncrementerDozensCount(object sender, EventArgs e)//事件处理程序的签名必须与委托的签名匹配
            {
                DozensCount++;
            }
        }
    }
14.6.1 通过扩展EventArgs来传递参数

要向事件处理程序的第二个参数传入数据,需要声明一个派生自EventArgs的自定义类,可以保存要传的数据。

static class Program
    {
        static void Main()
        {
            Incrementer incrementer = new Incrementer();
            Dozens dozensCounter = new Dozens(incrementer);

            incrementer.DoCount();
            Console.WriteLine("Number of dozens = {0}", dozensCounter.DozensCount);
            Console.ReadKey();
        }

        public class IncrementerEventArgs : EventArgs//自定义类派生自EventArgs
        {
            public int IterationCount { get; set; }//存储一个整数
        }

        class Incrementer
        {
            public event EventHandler<IncrementerEventArgs> CountADozen;//使用、自定义类的泛型委托

            public void DoCount()
            {
                IncrementerEventArgs args = new IncrementerEventArgs();//自定义类对象
                for (int i = 1; i < 100; i++)
                {
                    if (i % 12 == 0 && CountADozen != null)
                    {
                        CountADozen(this, args);//触发事件时传递参数
                    }
                }
            }
        }

        class Dozens
        {
            public int DozensCount { get; private set; }

            public Dozens(Incrementer incrementer)
            {
                DozensCount = 0;
                incrementer.CountADozen += IncrementerDozensCount;
            }

            void IncrementerDozensCount(object sender, IncrementerEventArgs e)//事件处理程序的签名必须与委托的签名匹配
            {
                Console.WriteLine("Incremented at iteration: {0} in {1}", e.IterationCount, sender.ToString());

                DozensCount++;
            }
        }
    }
上一篇下一篇

猜你喜欢

热点阅读