如何在C#中正确触发事件

时间:2019-05-01 06:51:38

标签: c#

我试图在单击按钮时触发一个事件。 我单击按钮,没有任何反应。 问题是我总是在OnEventA()中以EventA的形式收到null:

namespace eventsC
{
    public partial class Form1 : Form
    {

        public event EventHandler EventA;
        protected void OnEventA()
        {
            if (EventA != null)
                //never arrives here as EventA is allways = null
                EventA(this, EventArgs.Empty);
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OnEventA();
        }
    }
}

EDIT -----------------

基于Henk Holterman的链接,我还测试了此代码:

public delegate void Eventhandler(object sender, Eventargs args);  

// your publishing class
class Foo  
{
    public event EventHandler Changed;    // the Event

    protected virtual void OnChanged()    // the Trigger method, called to raise the event
    {
        // make a copy to be more thread-safe
        EventHandler handler = Changed;   

        if (handler != null)
        {
            // invoke the subscribed event-handler(s)
            handler(this, EventArgs.Empty);  
        }
    }

    // an example of raising the event
    void SomeMethod()
    {
       if (...)        // on some condition
         OnChanged();  // raise the event
    }
}

当我单击按钮时,我调用Onchaged(),但结果始终相同。 EventA = null。

2 个答案:

答案 0 :(得分:0)

我以这种方式解决了我的问题:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace eventsC
{    

    public partial class Form1 : Form
    {

        public static event EventHandler<EventArgs> myEvent;

        protected void OnMyEvent()
        {
            if (myEvent != null)
                myEvent(this, EventArgs.Empty);
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myEvent += Handler;

            //call all methods that have been added to the event
            myEvent(this, EventArgs.Empty);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OnMyEvent();
        }


        static void Handler(object sender, EventArgs args)
        {
            Console.WriteLine("Event Handled!");
        }

    }
}

答案 1 :(得分:0)

我认为他们已经正确回答它没有任何订户。

namespace TestConsoleApp
{
    public delegate void Eventhandler(object sender, EventArgs args);

    // your publishing class
    class Foo
    {
        public event EventHandler Changed;    // the Event

        protected virtual void OnChanged()    // the Trigger method, called to raise the event
        {
            // make a copy to be more thread-safe
            EventHandler handler = Changed;

            if (handler != null)
            {
                // invoke the subscribed event-handler(s)
                handler(this, EventArgs.Empty);
            }
        }

        // an example of raising the event
        public void SomeMethod()
        {

                OnChanged();  // raise the event
        }
    }

    public class Program
    {

        static void Main(string[] args)
        {
            Foo objFoo = new Foo();
            objFoo.Changed += ObjFoo_Changed;
            objFoo.SomeMethod();
            Console.ReadLine();
        }

        private static void ObjFoo_Changed(object sender, EventArgs e)
        {
            Console.WriteLine("Event fired and changed");
        }
    }
  }