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()メソッドをカプセル化できる参照の型を宣言するため、機能しません。そのため、 on を使用すると+= 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 に答える