2013-12-16 33 views
2

我正在尋求一些有助於理解事件的幫助。我一直在閱讀關於他們的文章並觀看教程視頻。尋找關於讓事件正常工作的建議

我幾乎瞭解他們,但我一直在碰碰運氣。

我使自己成爲一個簡單的WinForms測試應用程序來嘗試和學習過程。在應用程序中,屏幕上有兩個步行小精靈。 當你點擊表單時,它會創建一個下降thwomp的精靈(並創建一個事件),步行者精靈應該通過選擇一個離開精靈的新步行路徑來對事件做出反應。我認爲我已經正確地編寫了一切,但是當我編譯它時,出現以下錯誤:

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; 
    } 
} 
+1

將此類'RunEventArgs:EventArgs'更改爲'public class RunEventArgs:EventArgs' – Silvermind

回答

5

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 - 它會默認爲internal

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.

因此,使該RunEventArgspublic您的代碼應編譯。

+0

這會照顧編譯錯誤。當我點擊圖片框時,它不會觸發。我收到一個錯誤「對象引用未設置爲對象的實例。」因爲當我嘗試調用方法「TimeToRun(this,re); //觸發事件。」它是說TimeToRun是一個空對象。建議? – EtanSivad