简体   繁体   English

两个类之间的C#委托和事件处理程序

[英]c# delegate and event handler between two classes

I'm learning delegate and event hadler because i need in my code. 我正在学习委托和事件处理程序,因为我需要我的代码。 In the example, the Class Test generates the message Hello, i'm an event! 在示例中,Class Test生成消息Hello,我是事件!
From this Class i need to call the method Evento1 in Class Test1, to generate the message Hello, i'm another event! 从这个类中,我需要在类Test1中调用方法Evento1,以生成消息Hello,我是另一个事件! But i'm not able to do it. 但是我做不到。
I tryed to instantiate the Class Test1 and call the method. 我试图实例化Class Test1并调用该方法。 The debuger compile but the second message don't appear. 调试器编译,但是第二条消息没有出现。

namespace Eventi1
{
    // Delegate declaration
    delegate void testEventHandler(object source, string message);
    class Test
    {
        // Event
        public event testEventHandler testEvento;

        // Function that triggers the event
        public void Evento(string message)
        {
            Console.WriteLine("Event!");
            if (testEvento != null)
            { testEvento(this, message); }

            Test1 evento = new Test1();
            evento.Evento1("Hello, i'm another event!");
        }
    }

    class Test1
    {
        // Event1
        public event testEventHandler testEvento;

        // Function that triggers the event
        public void Evento1(string message)
        {
            Console.WriteLine("Event1!");
            if (testEvento != null)
            { testEvento(this, message); }
        }
    }

    public class EventSample
    {
        // Constructor
        public EventSample() { }
        static void Main()
        {
            // Classe instantiation
            Test b = new Test();

            // Event handler
            //b.testEvento += new testEventHandler(onTestAction);
            b.testEvento += onTestAction;

            // Event invocation
            b.Evento("Hello, i'm an event!");
        }

        // Function that triggers the event
        public static void onTestAction(object source, string message)
        {
            Console.WriteLine("We're inside the event handler.");
            Console.WriteLine("Messagge: " + message);
            Console.ReadLine();
        }
    }
}

PS second question to Aansari PS对Aansari的第二个问题

class Test
{
    public delegate void testEventHandler(object source, string message);
    public event testEventHandler testEvento;

    // Function that triggers the event
    public void Evento(string message)
    {
        Console.WriteLine("Event!");
        if (testEvento != null)
        { testEvento(this, message); }

        Test1 evento = new Test1();
        evento.testEvento += testEvento;
        evento.Evento1();
    }
}

class Test1
{
    public delegate void testEventHandler(object source, string message);
    public event testEventHandler testEvento;

    // Function that triggers the event1
    public void Evento1()
    {
        Console.WriteLine("Event1!");
        if (testEvento != null)
        { testEvento(this, "Hello, i'm another event!"); }
    }
}

public class EventSample
{
    public EventSample() { }
    static void Main()
    {
        Test b = new Test();
        b.testEvento += onTestAction;
        b.Evento("Hello, i'm an event!");
    }

    // Function that triggers the event
    public static void onTestAction(object source, string message)
    {
        Console.WriteLine("We're inside the event handler.");
        Console.WriteLine("Messagge: " + message + "\n");
        Console.WriteLine("Press ENTER");
        Console.ReadLine();
    }
}

Incorporate the following change and it will give you expected result 合并以下更改,它将为您带来预期的结果

Test1 evento = new Test1();
evento.testEvento += testEvento;
evento.Evento1("Hello, i'm another event!");

The problem in your code was, though you were assigning event handling method to Test class event handler, you were not assigning any event handling method to Test1 class event handler. 代码中的问题是,尽管您将事件处理方法分配给Test类事件处理程序,但没有将任何事件处理方法分配给Test1类事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM