程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> .NET 事件

.NET 事件

編輯:C#入門知識

事件概述                                                           

  • 特點

事件的訂閱和取消訂閱                                       

  • 訂閱事件
  • 
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    • 取消訂閱
    
    
    
    
    
    
    

    發布標准事件                                           

    
    
    
    
    
    
    
    • 采用 EventHandler 模式發布事件
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    引發派生類中的基類事件                                      

     BaseClassEvents
    {
         System;
         System.Collections.Generic;
          ShapeEventArgs : EventArgs
        {
              newArea;
    
             ShapeEventArgs( d)
            {
                newArea = d;
            }
              NewArea
            {
                 {  newArea; }
            }
        }
           Shape
        {
              area;
    
              Area
            {
                 {  area; }
                 { area = value; }
            }
              EventHandler<ShapeEventArgs> ShapeChanged;
               Draw();
               OnShapeChanged(ShapeEventArgs e)
            {
                EventHandler<ShapeEventArgs> handler = ShapeChanged;
                 (handler != )
                {
                    handler(, e);
                }
            }
        }
          Circle : Shape
        {
              radius;
             Circle( d)
            {
                radius = d;
                area = 3.14 * radius;
            }
              Update( d)
            {
                radius = d;
                area = 3.14 * radius;
                OnShapeChanged( ShapeEventArgs(area));
            }
               OnShapeChanged(ShapeEventArgs e)
            {
                .OnShapeChanged(e);
            }
               Draw()
            {
                Console.WriteLine();
            }
        }
          Rectangle : Shape
        {
              length;
              width;
             Rectangle( length,  width)
            {
                .length = length;
                .width = width;
                area = length * width;
            }
              Update( length,  width)
            {
                .length = length;
                .width = width;
                area = length * width;
                OnShapeChanged( ShapeEventArgs(area));
            }
               OnShapeChanged(ShapeEventArgs e)
            {
                .OnShapeChanged(e);
            }
               Draw()
            {
                Console.WriteLine();
            }
    
        }
          ShapeContainer
        {
            List<Shape> _list;
    
             ShapeContainer()
            {
                _list =  List<Shape>();
            }
    
              AddShape(Shape s)
            {
                _list.Add(s);
                s.ShapeChanged += HandleShapeChanged;
            }
              HandleShapeChanged( sender, ShapeEventArgs e)
            {
                Shape s = (Shape)sender;
                Console.WriteLine(, e.NewArea);
                s.Draw();
            }
        }
         Test
        {
    
              Main([] args)
            {
                Circle c1 =  Circle(54);
                Rectangle r1 =  Rectangle(12, 9);
                ShapeContainer sc =  ShapeContainer();
                sc.AddShape(c1);
                sc.AddShape(r1);
                c1.Update(57);
                r1.Update(7, 7);
                Console.WriteLine();
                Console.WriteLine();
                Console.ReadLine();
            }
        }
    }
    

    實現接口事件                                            

    • 在類中實現接口事件
    
    
    
    
    
    

     WrapTwoInterfaceEvents
    {
         System;
          IDrawingObject
        {
             EventHandler OnDraw;
        }
          IShape
        {
             EventHandler OnDraw;
        }
          Shape : IDrawingObject, IShape
        {
             EventHandler PreDrawEvent;
             EventHandler PostDrawEvent;
             EventHandler IDrawingObject.OnDraw
            {
                add { PreDrawEvent += value; }
                remove { PreDrawEvent -= value; }
            }
             EventHandler IShape.OnDraw
            {
                add { PostDrawEvent += value; }
                remove { PostDrawEvent -= value; }
            }
              Draw()
            {
                EventHandler handler = PreDrawEvent;
                 (handler != )
                {
                    handler(,  EventArgs());
                }
                Console.WriteLine();
                handler = PostDrawEvent;
                 (handler != )
                {
                    handler(,  EventArgs());
                }
            }
        }
          Subscriber1
        {
             Subscriber1(Shape shape)
            {
                IDrawingObject d = (IDrawingObject)shape;
                d.OnDraw +=  EventHandler(d_OnDraw);
            }
             d_OnDraw( sender, EventArgs e)
            {
                Console.WriteLine();
            }
        }
          Subscriber2
        {
             Subscriber2(Shape shape)
            {
                IShape d = (IShape)shape;
                d.OnDraw +=  EventHandler(d_OnDraw);
            }
    
             d_OnDraw( sender, EventArgs e)
            {
                Console.WriteLine();
            }
        }
          Program
        {
              Main([] args)
            {
                Shape shape =  Shape();
                Subscriber1 sub =  Subscriber1(shape);
                Subscriber2 sub2 =  Subscriber2(shape);
                shape.Draw();
    
                Console.WriteLine();
                Console.ReadLine();
            }
        }
    }
    

    使用字典存儲事件實例                                       

       EventHandler1( i);
       EventHandler2( s);
      PropertyEventsSample
    {
         System.Collections.Generic.Dictionary<, System.Delegate> eventTable;
         PropertyEventsSample()
        {
            eventTable =  System.Collections.Generic.Dictionary<, System.Delegate>();
            eventTable.Add(, );
            eventTable.Add(, );
        }
          EventHandler1 Event1
        {
            add
            {
                eventTable[] = (EventHandler1)eventTable[] + value;
            }
            remove
            {
                eventTable[] = (EventHandler1)eventTable[] - value;
            }
        }
          EventHandler2 Event2
        {
            add
            {
                eventTable[] = (EventHandler2)eventTable[] + value;
            }
            remove
            {
                eventTable[] = (EventHandler2)eventTable[] - value;
            }
        }
          RaiseEvent1( i)
        {
            EventHandler1 handler1;
             ( != (handler1 = (EventHandler1)eventTable[]))
            {
                handler1(i);
            }
        }
          RaiseEvent2( s)
        {
            EventHandler2 handler2;
             ( != (handler2 = (EventHandler2)eventTable[]))
            {
                handler2(s);
            }
        }
    }
      TestClass
    {
           Delegate1Method( i)
        {
            System.Console.WriteLine(i);
        }
           Delegate2Method( s)
        {
            System.Console.WriteLine(s);
        }
          Main()
        {
            PropertyEventsSample p =  PropertyEventsSample();
    
            p.Event1 +=  EventHandler1(TestClass.Delegate1Method);
            p.Event1 +=  EventHandler1(TestClass.Delegate1Method);
            p.Event1 -=  EventHandler1(TestClass.Delegate1Method);
            p.RaiseEvent1(2);
    
            p.Event2 +=  EventHandler2(TestClass.Delegate2Method);
            p.Event2 +=  EventHandler2(TestClass.Delegate2Method);
            p.Event2 -=  EventHandler2(TestClass.Delegate2Method);
            p.RaiseEvent2();
        }
    }
    

    事件的異步模式                            

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved