【问题标题】:Looking for advice on getting events to work寻求有关使活动正常进行的建议
【发布时间】:2013-12-16 23:06:29
【问题描述】:

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

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

我为自己制作了一个简单的 WinForms 测试应用程序来尝试学习该过程。在应用程序中,有 2 个行走的精灵在屏幕周围运行。 当您单击表单时,它会创建一个落下的 thwomp 的精灵(并且它会创建一个事件),步行者精灵应该通过选择远离精灵的新步行路径来对事件做出反应。我认为我已经正确编写了所有内容,但是当我编译它时出现错误:

错误 1 ​​可访问性不一致:参数类型“eventStomper.RunEventArgs”的可访问性低于委托“eventStomper.RunInFear”
错误 2 可访问性不一致:参数类型“eventStomper.RunEventArgs”的可访问性低于方法“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;
    }
}

【问题讨论】:

  • 把这个class RunEventArgs : EventArgs改成public class RunEventArgs : EventArgs

标签: c# events


【解决方案1】:

我不知所措,因为一切都是公开的。

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

参数类型“eventStomper.RunEventArgs”比委托“eventStomper.RunInFear”更难访问

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

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

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

class RunEventArgs : EventArgs 
{
    public Point _pointOfFear;
}

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

不嵌套在其他类型中的顶级类型只能具有内部或公共可访问性。这些类型的默认可访问性是内部的。

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

【讨论】:

  • 这会处理编译错误。当我单击图片框时,它没有触发。我收到错误消息“对象引用未设置为对象的实例。”因为当我尝试调用方法“TimeToRun(this, re); //触发事件。”它是说 TimeToRun 是一个空对象。有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-26
  • 2014-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多