繁体   English   中英

C#EventHandler在调用时返回null

[英]C# EventHandler returns null when called

我有两节课,在一节课中我创建并引发了一个事件,如下所示:

客户添加类别

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);
 }
}
}

MyWindow类

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类调用SaveToDisk方法时(因为我正在将DisplayDetails方法订阅到ListUpDated事件中),因此不会调用DisplayDetails 调试器显示ListUpdated为null。 我已经搜索了几个小时,但未能提出解决方案。我点击了此链接,ListUpdated仍然为空。 任何指导/帮助将不胜感激。

尝试这个:

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();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM