المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Query on Delegate



C# Programming
07-12-2012, 12:21 PM
I am going through one code and trying to understand the same.

There are two code file 1) PuzzleGame.cs and 2) PuzzlePage.cs, each contains class each and PuzzlePage.cs files consumes the class defined in PuzzleGame.cs.

Beloow is the snippet of code from both class, I have query on the bold code surrounded with the "*" and trying to understand what does it mean and how it needs to be interpreted.

Can someone make me understand in simple terms how this "+=" delgate is interpreted in the code and how it might be functioning.

1) PuzzleGame.cs:
public class PuzzleGame
{
private int colsAndRows;
private int[] board;
private int totalMoves;
private bool isPlaying;

public int ColsAndRows { get { return this.colsAndRows; } }
public int TotalMoves { get { return this.totalMoves; } }
public bool IsPlaying { get { return this.isPlaying; } }
public int[] BoardPieces { get { return this.board; } }


public PuzzleGame(int colsAndRows)
{
if (colsAndRows < 2)
{
throw new ArgumentOutOfRangeException("colsAndRows");
}

this.colsAndRows = colsAndRows;
int totalPieces = colsAndRows * colsAndRows;
this.board = new int[totalPieces];
this.Reset();
}

public EventHandler GameStarted;
public EventHandler PieceUpdated;
public EventHandler GameOver;

>>>>>>>>> further code after this.

2) PuzzlePage:
Constructor code snippet:

***************************this.game.GameStarted += delegate { this.ResetWinTransition.Begin(); this.StatusPanel.Visibility = Visibility.Visible; this.TapToContinueTextBlock.Opacity = 0; this.TotalMovesTextBlock.Text = this.game.TotalMoves.ToString(); };************************** ****************************** this.game.GameOver += delegate { this.WinTransition.Begin(); this.TapToContinueTextBlock.Opacity = 1; this.StatusPanel.Visibility = Visibility.Visible; this.TotalMovesTextBlock.Text = this.game.TotalMoves.ToString(); };******************************* ********************** this.game.PieceUpdated += delegate(object sender, PieceUpdatedEventArgs args) { int pieceSize = ImageSize / this.game.ColsAndRows; this.AnimatePiece(this.puzzlePieces[args.PieceId], Canvas.LeftProperty, (int)args.NewPosition.X * pieceSize); this.AnimatePiece(this.puzzlePieces[args.PieceId], Canvas.TopProperty, (int)args.NewPosition.Y * pieceSize); this.TotalMovesTextBlock.Text = this.game.TotalMoves.ToString(); };**************************