2012-12-02 126 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"); 
     } 
    }