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

时间:2017-06-09 17:10:45

标签: c# class delegates event-handling

我正在学习委托和事件hadler,因为我需要在我的代码中。在示例中,Class Test生成消息 Hello,我是一个事件!
从这个类我需要调用类Test1中的方法Evento1,以生成消息您好,我是另一个活动!但是我无法做到。
我尝试实例化Class Test1并调用该方法。 debuger编译但第二条消息没有出现。

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对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();
    }
}

1 个答案:

答案 0 :(得分:2)

合并以下更改,它将为您提供预期结果

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

代码中的问题是,虽然您将事件处理方法分配给Test类事件处理程序,但您没有将任何事件处理方法分配给Test1类事件处理程序。