Search code examples
windows-phone-7messagebox

Multiples MessageBoxes - WP7


I have a game with 15 levels and when we finish a level it shows a MessageBox with the player's situation, before app navigates to a page with the next level objective.

However, that messageBox don't appears only one time, it's is displayed the number of times we play. For example, if we play the first level appears one time, but if we play the second level or if we repeat the first level the messageBox is displayed two times.

Why it happens? And how I resolve it?

This is my code:

void model_finishGame(bool playerWin)
{
    timer.Stop();

    if(playerWin==true)
       if(MessageBox.Show("Sucess!!! You Win")==MessageBoxResult.OK)
           NavigationService.Navigate(new Uri("/LevelObjective.xaml", UriKind.Relative));
    else
       if(MessageBox.Show("Sorry, you lose")==MessageBoxResult.OK)
           NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}

Solution

  • Probably the reason is that you probably register for an event at the start of a level (maybe the timer event?) and never unregistering it so each time you start the level the event handler is called as many time as it has been registered (and I'm guessing the message box is shown from this event handler)