Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#事件即使已订阅,也为空_C#_Events - Fatal编程技术网

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

C#事件即使已订阅,也为空,c#,events,C#,Events,我有一个Flight类和一个Form类,我想将日志消息从Flight类发送到表单的文本字段 我已经为另一个名为Airport的类创建了一个可用的类,但是这个类实际上是相同的,但是事件LogMessage始终为空,即使在订阅之后也是如此 --主要形式-- --飞行-- 那么,即使订阅了事件,事件仍为空的原因是什么呢?编辑:此MCVE工作正常 Program.cs namespace StackOverflowPlayground { class Program {

我有一个Flight类和一个Form类,我想将日志消息从Flight类发送到表单的文本字段

我已经为另一个名为Airport的类创建了一个可用的类,但是这个类实际上是相同的,但是事件
LogMessage
始终为空,即使在订阅之后也是如此

--主要形式--

--飞行--


那么,即使订阅了事件,事件仍为空的原因是什么呢?

编辑:此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);
            }
        }
    }

}

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

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

你应该没事。

你确定在
初始化事件()之前没有调用
StartFlight()
?例如,在
Form_Load()
?@jeroenmoster中,他没有提到它抛出了
NullReferenceException
,可能是因为他检查了它。它仍然可以为null。@JeroenMostert:OP在这段代码中没有提到
NullReferenceException
,而是在他的最后一个问题中提到了。因此,我假设这个代码片段已经阻止了异常,但是EventHandler
LogMessage
仍然为空。所以问题是无法引发该事件。@DionV。是的,我非常确定,因为当我按下按钮时会调用StartFlight(),并且在表单创建过程中会调用InitializeEvents。我马上会调查MCVE的事情,你确定你没有在别的地方创建新的
航班吗?让我觉得您可能会这样做,因为您有一个带参数的构造函数,但您正在创建(并使用)一个没有参数的构造函数。
this.flight.LogMessage+=new System.EventHandler(OnLogReceived)在第一次调用OnLogUpdate()之前,您确定要调用它吗?您是否设置了断点并验证了它是否按照您希望的方式设置?@CBauer是的,在initializeEvents和OnLogUpdate()上添加断点会首先在initializeEvents上中断,然后在OnLogUpdate上中断。当我按下一个按钮set以调用OnLogUpdate()时,您的代码正在执行不同的操作,因为我刚刚在一个控制台应用程序中实现了它,它工作得很好(请参阅我的编辑)。@CBauer好的,谢谢,我会再看一遍,但是谢谢=)谢谢,这很有道理:)
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);
            }
        }
    }

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