c# - 寻求有关开展事件的建议

标签 c# events

我正在寻求一些帮助来理解事件。我一直在阅读有关它们的文章并观看教程视频。

我几乎理解了它们,但我总是遇到障碍。

我为自己制作了一个简单的 WinForms 测试应用程序来尝试学习该过程。在应用程序中,有 2 个行走的 Sprite 在屏幕上奔跑。 当您单击表单时,它会创建一个下落重击的 Sprite (并创建一个事件),步行者 Sprite 应该通过选择远离 Sprite 的新行走路径来对该事件使用react。我认为我已经正确编写了所有内容,但是当我编译它时,我收到错误:

Error 1 Inconsistent accessibility: parameter type 'eventStomper.RunEventArgs' is less accessible than delegate 'eventStomper.RunInFear'
Error 2 Inconsistent accessibility: parameter type 'eventStomper.RunEventArgs' is less accessible than method 'eventStomper.Walker.RunAway(object, eventStomper.RunEventArgs)'

我很茫然,因为一切都是公开的。对那里的错误有什么建议吗?并且,对事件处理有什么建议吗?

这里的源代码归结为相关部分:

namespace eventStomper
{

    public delegate void RunInFear(object sender, RunEventArgs re); //The delegate for responding to events.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            spawnWalkers();  //Create a couple of walkers to roam around the form  

        }
        List<Thwomp> thowmpList = new List<Thwomp>(); //List of thwomps.  This is iterated through for animation.
        List<Walker> walkerList = new List<Walker>();// Same thing with the walkers.

        public void pictureBox1_Click(object sender, EventArgs e) //When you click on the background, it spawns a thwomp
        {
            Point _spawnPoint = this.PointToClient(Cursor.Position);


            Thwomp _thwomp = new Thwomp(_spawnPoint,  sprite );  //Generate a new Thwomp
            thowmpList.Add(_thwomp); //Add it to the list of Thwomps
            _thwomp.TimeToRun += walkerList[0].RunAway; //Register with the two walkers roaming around.
            _thwomp.TimeToRun += walkerList[1].RunAway;

            //Do other things to setup the thwomp sprite

        }


    }

   public class Thwomp
    {
        public int spriteX = 0;//Current sprite location
        public int spriteY = 0;
        public int targetX = 0;//Where the thwomp will land.
        public int targetY = 0;


        public event RunInFear TimeToRun;

      public void Animate()
        {
            //Do Animation steps.
        }
        public Thwomp(Point spawnPoint,  PictureBox spriteIncoming)
        {
            RunEventArgs re = new RunEventArgs();
            re._pointOfFear = spawnPoint;

            //Setup thwomp sprite 
            TimeToRun(this, re); //Trigger the event.
        }
    }

    public class Walker
    {
        public int spriteX = 0;  //Current sprite location
        public int spriteY = 0;

        public Walker(Point spawnPoint, PictureBox spriteIncoming)
        {
                //Create the walker 
        }

        public void RunAway(Point dangerPoint)
        {
            if (Math.Abs(sprite.Top - dangerPoint.Y) < 20 && Math.Abs(sprite.Left - dangerPoint.X) < 20) //If near a newly created thwomp, run away.
            {
                 //Pick a path headed away from the danger.  
            }
        }

        public void Animate()
        {
            //Move the walker away.
        }
    }

    class RunEventArgs : EventArgs 
    {
        public Point _pointOfFear;
    }
}

最佳答案

I'm at a loss because everything is public.

不完全是。正如错误消息所示:

parameter type 'eventStomper.RunEventArgs' is less accessible than delegate 'eventStomper.RunInFear'

根据该消息,RunEventArgsRunInFear 更难访问。因此,让我们看看这两种类型的可访问性级别:

public delegate void RunInFear(object sender, RunEventArgs re);

所以,这是公开的。到目前为止,一切顺利。

class RunEventArgs : EventArgs 
{
    public Point _pointOfFear;
}

啊哈!这个没有分配可访问性,这意味着 - 根据docs - 它将默认为内部:

Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.

因此,将 RunEventArgs 类设为 public 并且您的代码应该可以编译。

关于c# - 寻求有关开展事件的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20622796/

相关文章:

javascript - jQuery preventDefault() 问题

jQuery:不同 parent 之间的事件委托(delegate)

c# - 枚举列表 <Object> (Id, Name)

c# - neo4j REST API 性能不佳

c# - 如何从一个 ASP.NET 页面重定向到另一个

Javascript DOM Onclick 事件触发单个元素

c# - 在 .NET 中,是否可以根据函数调用设置事件

c# - 获取两个文件的差异

c# - 尝试从包含 UNION 的 View 中进行 SELECT 时出现 DB2 'SQL0901 SQL System Error.'

java - 如何向在另一个按钮内创建的按钮添加功能?