This is the second part where I will add the Application bar (programatically) to the Tic Tac Toe game to start a new game after the game is over. We will add a simple Application bar button. This is part 2 of the blog. If you are not familiar with this, please check out the Programming Tic Tac Toe Part 1.
This a very simple step. On Mainpage I will add a Application bar and create a button with a Icon. then I will add a click event handler to the button. When the button is clicked, the StartGame() method will run and it will reinitialize everything and you will have a new game. Very Short and Sweet.
- public MainPage()
-         {
-             InitializeComponent();
-             //add the application bar
-             ApplicationBar = new ApplicationBar();
-             ApplicationBarIconButton button1 = new ApplicationBarIconButton(new Uri("/images/NewGame.ico", UriKind.Relative));
-             button1.Text = "New Game";
-             ApplicationBar.Buttons.Add(button1);
-             button1.Click += new EventHandler(button1_Click);
-           ÂÂ
-             //Starts the game
-             StartGame();
-         }
Here is the Click event handler
- void button1_Click(object sender, EventArgs e)
-         {
-             StartGame();
-         }
And here is the StartGame method.
- /// <summary>
-         /// Starts the game.
-         /// </summary>
-         private void StartGame()
-         {
-             player1Moves = new List<int>();
-             player2Moves = new List<int>();
-             _isGameOver = false;
-             _notAvailablePositions = new List<int>();
-             _nextMoveFor = (int)Players.Player1;
-             this.TopLeft1.Content = string.Empty;
-             TopMiddle1.Content = string.Empty;
-             TopRight1.Content = string.Empty;
-             MiddleLeft1.Content =  string.Empty;
-             MiddleMiddle1.Content = string.Empty;
-             buMiddleRight1.Content = string.Empty;
-             BottomLeft1.Content = string.Empty;
-             BottomMiddle1.Content = string.Empty;
-             BottomRight1.Content = string.Empty;
-             textBox1.Text = "New Game";
-         }
So now when you plug all these codes in you will get this.
See that little application bar on the bottom, that is what we added. When you click on that, it starts a new game.