C# EventHandler returns 调用时为空

C# EventHandler returns null when called

我有两个 classes.In 一个 class 我正在创建和引发一个事件,如下所示:

客户添加 Class

public class CustomerAdd
{
public delegate void Done(object Sender, EventArgs e);
public event Done ListUpdated;

public void UpdateNewList()
{
 //adding items to a generic List<T>,code removed as not relevant to post
 //and raising the event afterwards

 if (ListUpdated != null)
 {
  ListUpdated(this, EventArgs.Empty);
 }
}
}

我的窗口Class

public class MyWindow
{
private void SaveToDisk()
 {
  CustomerAdd cuss = new CustomerAdd();
  cuss.ListUpdated += new CustomerAdd.Done(DisplayDetails);
  cuss.UpdateNewList();
 }
 private void DisplayDetails()
 {
  //other codes here
 }
}

现在,当我从 MyWIndow class 调用 SaveToDisk 方法时(因为我将 DisplayDetails 方法订阅到 ListUpDated 事件), DisplayDetails 未被调用。调试器显示 ListUpdated 为空。我已经搜索了几个小时,但未能找到 solution.I 后跟 this linkListUpdated 仍然为空。任何 guidance/help 将不胜感激。

试试这个:

using System;

namespace ConsoleApp1
{
    class Program
    {

        static void Main(string[] args)
        {
            CustomerReceive cr = new CustomerReceive();
            cr.SaveToDisk();

        }
    }

    public class CustomerAdd
    {
        public delegate void Done(object Sender, EventArgs e);
        public event Done ListUpdated;

        public void UpdateNewList()
        {
            //adding items to a generic List<T>,code removed as not relevant to post
            //and raising the event afterwards

            if (ListUpdated != null)
            {
                ListUpdated.Invoke(this, EventArgs.Empty);
            }
        }
    }

    public class CustomerReceive
    {
        public void SaveToDisk()
        {
            CustomerAdd cuss = new CustomerAdd();
            cuss.ListUpdated += new CustomerAdd.Done(DisplayDetails);
            cuss.UpdateNewList();
        }
        private void DisplayDetails(object Sender, EventArgs e)
        {
            int k = 0;
        }
    }
}

你需要仔细阅读委托和事件,因为当有更多的听众时这不起作用

有效:

using System;

namespace ConsoleApp2
{
    class Program
    {

        public class CustomerAdd1
        {
            public delegate void Done(object Sender, EventArgs e);
            public event Done ListUpdated;

            public void UpdateNewList()
            {
                //adding items to a generic List<T>,code removed as not relevant to post
                //and raising the event afterwards

                if (ListUpdated != null)
                {
                    ListUpdated(this, EventArgs.Empty);
                }
            }
        }

        public class CustomerAdd
        {
            public void SaveToDisk()
            {
                CustomerAdd1 cuss = new CustomerAdd1();
                cuss.ListUpdated += new CustomerAdd1.Done(DisplayDetails);
                cuss.UpdateNewList();
            }
            private void DisplayDetails(object Sender, EventArgs e)
            {
                Console.WriteLine("Test");
            }
        }

        static void Main(string[] args)
        {
            var c = new CustomerAdd();
            c.SaveToDisk();
            Console.ReadLine();
        }
    }
}