即使订阅了C#事件也是null

时间:2014-12-11 15:00:15

标签: c# events

我有一个Flight类和一个Form类,我想将Log消息发送到从Flight类到Form的文本字段。

我已经为另一个名为Airport的类工作了,但是这个类实际上是相同的,但事件LogMessage总是为空,即使在订阅之后也是如此。

- MainForm -

namespace FlightSim
{
    public partial class MainForm : Form
    {
        Airport airport = new Airport();
        Luggage luggage = new Luggage();
        Flight flight = new Flight();
        DAO db = new DAO();

        public MainForm()
        {
            InitializeComponent();
            InitializeEvents();
        }

        private void InitializeEvents()
        {
            this.airport.ErrorMessage += new System.EventHandler(OnErrorReceived);
            this.flight.LogMessage += new System.EventHandler(OnLogReceived);
        }

        public void OnErrorReceived(object sender, System.EventArgs e)
        {
            string msgContent = ((Airport.MessageEventArgs)e).msgContent;
            this.mainLog.AppendText(msgContent);
        }

        public void OnLogReceived(object sender, System.EventArgs e)
        {
            string msgcontent = ((Flight.MessageEventArgs)e).msgContent;
            this.mainLog.AppendText(msgcontent);
        }
    }
}

- 飞行 -

namespace FlightSim
{
    public class Flight
    {
        public class MessageEventArgs : System.EventArgs
        {
            public string msgContent;
        }

        public event System.EventHandler LogMessage;
        DAO db = new DAO();

        public Flight(string flightNumber, string departure, string destination, int totalLoadCapacity)
        {
            this.FlightNumber = flightNumber;
            this.Departure = departure;
            this.Destination = destination;
            this.TotalLoadCapacity = totalLoadCapacity;
            //LogMessage += (s, o) => { };
        }

        public void StartFlight()
        {
            string tmpDeparture = this.Departure;
            string tmpDestination = this.Destination;
            this.OnLogUpdate("Taking off from " + tmpDeparture + " now.");
            this.Destination = tmpDeparture;
            Thread.Sleep(1000);
            this.OnLogUpdate("Arriving in " + tmpDestination + " now.");
            this.Departure = tmpDestination;
        }

        protected void OnLogUpdate(string logMessage)
        {
            if (logMessage == "")
                return;

            MessageEventArgs e = new MessageEventArgs();
            var handler = LogMessage;

            if (handler != null)
            {
                e.msgContent = logMessage;
                handler(this, e);
            }
        }
    }
}

那么,即使订阅了活动,null也可能是什么原因?

2 个答案:

答案 0 :(得分:1)

编辑:此MCVE正常运行

Program.cs的

namespace StackOverflowPlayground
{
    class Program
    {

        static void Main(string[] args)
        {
            var sim = new AirportSim();
            sim.flight.StartFlight();
        }
    }
}

FlightSim.cs

using System;
using System.Threading;

namespace StackOverflowPlayground
{
        public class AirportSim
        {
            public Flight flight = new Flight("1","","",1);

            public AirportSim()
            {
                InitializeEvents();
            }

            private void InitializeEvents()
            {

                flight.LogMessage += OnLogReceived;
            }

            public void OnLogReceived(object sender, System.EventArgs e)
            {
                string msgcontent = ((Flight.MessageEventArgs)e).msgContent;
                Console.WriteLine(msgcontent);
            }
        }
    public class Flight
    {
        public class MessageEventArgs : EventArgs
        {
            public string msgContent;
        }

        public event EventHandler LogMessage;

        public Flight(string flightNumber, string departure, string destination, int totalLoadCapacity)
        {
            FlightNumber = flightNumber;
            Departure = departure;
            Destination = destination;
            TotalLoadCapacity = totalLoadCapacity;
            //LogMessage += (s, o) => { };
        }

        public string Destination { get; set; }

        public int TotalLoadCapacity { get; set; }

        public string Departure { get; set; }

        public string FlightNumber { get; set; }

        public void StartFlight()
        {
            string tmpDeparture = this.Departure;
            string tmpDestination = this.Destination;
            OnLogUpdate("Taking off from " + tmpDeparture + " now.");
            Destination = tmpDeparture;
            Thread.Sleep(1000);
            OnLogUpdate("Arriving in " + tmpDestination + " now.");
            Departure = tmpDestination;
        }

        protected void OnLogUpdate(string logMessage)
        {
            if (logMessage == "")
                return;

            var e = new MessageEventArgs();
            var handler = LogMessage;

            if (handler != null)
            {
                e.msgContent = logMessage;
                handler(this, e);
            }
        }
    }

}

答案 1 :(得分:1)

给定带有参数的构造函数和没有参数的初始化,您可能正在其他地方创建另一个Flight类。您所要做的就是确保在创建时订阅相同的事件。做这样的事情;

Flight someOtherFlight = new Flight("1", "Amsterdam", "Hong Kong", 500);
someOtherFlight.LogMessage += new System.EventHandler(OnLogReceived);

你应该没事。