Hooking up events to phone application status failed


I’m using MonoGame to port my little pet XNA game project to WP8. I used some code from the Game State Management example, and it was working properly in the original XNA project. However, in the MonoGame project, the following code is not working (inside my custom game class):

        Microsoft.Phone.Shell.PhoneApplicationService.Current.Launching += new EventHandler<Microsoft.Phone.Shell.LaunchingEventArgs>(GameLaunching);
        Microsoft.Phone.Shell.PhoneApplicationService.Current.Activated += new EventHandler<Microsoft.Phone.Shell.ActivatedEventArgs>(GameActivated);
        Microsoft.Phone.Shell.PhoneApplicationService.Current.Deactivated += new EventHandler<Microsoft.Phone.Shell.DeactivatedEventArgs>(GameDeactivated);

The GameLaunching method adds the initial game screen. With the code above the initial screen was not added and there was only black screen. I tried calling the GameLaunching method directly instead, and the initial screen got loaded properly.

So here is my question: Is this a bug in MonoGame? If yes, is there a way around it?



MonoGame already uses those events to fire XNA’s Activated and Deactivated events. You could use those instead, since using PhoneApplicationService is not multiplatform. With Activated you will have to use a boolean to find out if it’s a first launch or a resume.

On the other hand, you could just add your initial game screen at the end of the Initialize method since that only gets called when you application is launching.

Thanks for your help Nezz! So how should I change those lines? Could you please post an example? (I’m pretty new to MonoGame…)

Oh I think I got your idea… I can call the GameLaunching/GameActivated/GameDeactivated methods in the Game.Activated and Game.Deactivated methods, right?

Exactly :slight_smile: Either call them from your override of OnActivated and OnDeactivated, or just hook 'em up to Activated and Deactivated (nicer).

Thanks so much Nezz! You saved my day! =)

Hi Nezz,

One more question: how do I tell if it’s a first launch or a resume?

Have a bool relaunch = false; in you Game class, then in the activated:

if (!this.relaunch)
    // First launch
    this.relaunch = true;

Thanks for the help again Nezz! I’m a little confused… The GSM sample used this function to activate the game:

    void GameActivated(object sender, Microsoft.Phone.Shell.ActivatedEventArgs e)
        // Try to deserialize the screen manager
        if (!screenManager.Activate(e.IsApplicationInstancePreserved))
            // If the screen manager fails to deserialize, add the initial screens

It seems to use Microsoft.Phone.Shell.ActivatedEventArgs.IsApplicationInstancePreserved to find out if the instance gets preserved when the game is tombstoned. So I guess the “relaunch” variable in your code does get saved during tombstoning, right? (sorry I’m not really familiar with the way phone app life cycle works…)

There is no WP7 pre-mango style tombstoning anymore in WP8. You can just ignore the whole activated/launch detection and add AddInitialScreens(); to the end of Initialize.

Thanks very much Nezz! =)