8

I am using Monogame Implementation of xna to develop a simple Air Hockey game in windows store, and I am navigating to some XAML page if any of the players attains a score of 7

this is the Logic I am using in Update method

 protected override void Update(GameTime gameTime)
        {
            DrawGame();
            if (player1.Score == 7)
            {
                try
                {
                    PlayerOneScore = player1.Score;
                    PlayerTwoScore = player2.Score;
                    App.WinningFlag = true;
                    App.WinningPlayer = PlayerOneName;
                    this.Exit();
                    if (BaseCount == 0)
                    {
                        //Navigation to next page 
                        BaseCount++;
                        Frame ff = new Frame();
                        ff.Navigate(typeof(NextPage));
                        Windows.UI.Xaml.Window.Current.Content = ff;
                        Windows.UI.Xaml.Window.Current.Activate();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            base.Update(gameTime);
        }

However I see that the methods Update and Draw are still getting called, Why so? Though I have written the code to navigate away from this page, Also in NextPage there is a button coded for restarting the game

Click event for button restart game in NextPage

private void btnStartNewGame_Click(object sender, RoutedEventArgs e)
        {
            var app = App.Current as App;
            if (app.GamePage != null)
            { app.GamePage = null; app.GamePage = new GamePage(string.Empty); }
            else { app.GamePage = new GamePage(string.Empty); }
            Window.Current.Content = app.GamePage;
        }

This renders me with a Black screen, No textures in game1.cs classes are drawn.

How can I navigate from a Game1.cs to Xaml and get Back to Game1.cs as a fresh call?

A.K.
  • 3,321
  • 1
  • 15
  • 27

1 Answers1

0

I got the solution to the problem. Though I dont know it is the correct/Ideal way but It worked for me at least.

the code I am writing to navigate back to game is wrong

Old and erroneous

 private void btnStartNewGame_Click(object sender, RoutedEventArgs e)
            {
                var app = App.Current as App;
                if (app.GamePage != null)
                { app.GamePage = null; app.GamePage = new GamePage(string.Empty); }
                else { app.GamePage = new GamePage(string.Empty); }
                Window.Current.Content = app.GamePage;
            }

Instead of creating an instance of GamePage every time you want to access it, you create it once and hold a reference to it in the App object. I am attaching the modified version as well:

Modified

        private void btnStartNewGame_Click(object sender, RoutedEventArgs e)
        {
        var app = App.Current as App;
        if (app.GamePage != null)
        Window.Current.Content = app.GamePage;
        }

also A link

This helped me, Hope it helps you too :)

A.K.
  • 3,321
  • 1
  • 15
  • 27