C# 事件为空,即使它已订阅

本文关键字:事件 | 更新日期: 2023-09-27 18:33:55

我有一个外部测试类和一个表单类,我想将日志消息发送到从飞行类到表单的文本字段。

我已经为另一个名为 Airport 的课程工作,但这个实际上是相同的,但即使在订阅后,事件LogMessage也始终为空。

--

主窗体 --

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的原因是什么?

C# 事件为空,即使它已订阅

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

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

你应该没事。

编辑:此MCVE工作正常

程序.cs

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

飞行模拟.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);
            }
        }
    }
}