2

我正在尝试创建一个可以以另一种形式访问的委托事件。但是主窗体看不到我的代表。它说代表名称此时无效。情态形式

public partial class GameOverDialog : Window
{
   public delegate void ExitChosenEvent();
   public delegate void RestartChosenEvent();

   public GameOverDialog()
   {
      InitializeComponent();
   }

   private void closeAppButton_Click(object sender, RoutedEventArgs e)
   {
      ExitChosenEvent exitChosen = Close;
      exitChosen();
      Close();
   }

   private void newGameButton_Click(object sender, RoutedEventArgs e)
   {
      RestartChosenEvent restart = Close;
      restart();
      Close();
   }
}

主要形式:

private void ShowGameOver(string text)
{
   var dialog = new GameOverDialog { tb1 = { Text = text } };
   dialog.RestartChosenEvent += StartNewGame();
   dialog.Show();
}

private void StartNewGame()
{
   InitializeComponent();
   InitializeGame();
}

在@Fuex 的帮助之后*

private void ShowGameOver(string text)
{
   var dialog = new GameOverDialog { tb1 = { Text = text } };
   dialog.RestartEvent += StartNewGame;
   dialog.ExitEvent += Close;
   dialog.Show();
}
4

2 回答 2

4

它不起作用,因为delegate void RestartChosenEvent()声明了允许封装方法的引用类型。因此,通过使用它+= StartNewGame会产生错误。正确的代码是:

public partial class GameOverDialog : Window
{
    delegate void myDelegate();

    public myDelegate RestartChosenEvent;
    public myDelegate ExitChosenEvent;

    public GameOverDialog()
    {
        InitializeComponent();
    }

    private void closeAppButton_Click(object sender, RoutedEventArgs e)
    {
        ExitChosenEvent();
        this.Close();
    }

    private void newGameButton_Click(object sender, RoutedEventArgs e)
    {
        RestartChosenEvent();
        this.Close();
    }
}

然后在您的主要形式中,您必须使用StartNewGame,这是要传递的方法的指针,而不是StartNewGame()

private void ShowGameOver(string text)
{
   var dialog = new GameOverDialog { tb1 = { Text = text } };
   dialog.RestartChosenEvent += StartNewGame;
   dialog.Show();
}
于 2012-12-02T18:41:28.173 回答
1

当您定义委托而不使用关键字时,也会出现“此时委托名称无效”错误。new例如:

// In asp.net the below would typically appear in a parent page (aspx.cs page) that 
// consumes a delegate event from a usercontrol:
    protected override void OnInit(EventArgs e) 
    { 
        Client1.EditClientEvent += Forms_Client.EditClientDelegate(Client1_EditClientEvent); 
        //NOTE: the above line causes 'delegate name is not valid at this point' error because of the lack of the 'new' keyword.
        Client1.EditClientEvent += new Forms_Client.EditClientDelegate(Client1_EditClientEvent); 
        //NOTE: the above line is the correct syntax (no delegate errors appear)
    } 

要将其放入上下文中,您可以在下面查看委托的定义。在 asp.net 中,如果您正在定义需要从其父页面(托管控件的页面)获取值的用户控件,那么您将在用户控件中定义您的委托,如下所示:

//this is the usercontrol (ascx.cs page):
    public delegate void EditClientDelegate(string num); 
    public event EditClientDelegate EditClientEvent; 
    //call the delegate somewhere in your usercontrol:
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (EditClientEvent != null) { 
            EditClientEvent("I am raised"); 
        }
    } 
于 2013-08-16T12:50:33.263 回答