2012-12-02 125 views
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

它不起作用,因为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(); 
} 
+0

我所做的更改,你可以看到上面。但它仍然不会调用StartNewGame(); –

+1

好的,我添加了一些代码。 –

+0

非常感谢! –

1

“代表的名字是此时无效'错误也可能发生在您定义委托并且未使用关键字newnew时。例如:

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