views:

120

answers:

2

I'm experiencing odd behavior in the wp7 emulator.

I have a dead simple app that's mostly directly from the template generated by VS 2010.

From App.xaml:

    <!--Required object that handles lifetime events for the application-->
    <shell:PhoneApplicationService 
        Launching="Application_Launching" Closing="Application_Closing" 
        Activated="Application_Activated" Deactivated="Application_Deactivated"/>

Tombstoning code from App.xaml.cs:

    private void LoadSettings()
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        ICollection<TicTacSpot> getSpots;
        if (settings.TryGetValue<ICollection<TicTacSpot>>("spots", out getSpots))
        {
            Spots = getSpots;
        }

        if (Spots == null)
        {
            Spots = new List<TicTacSpot>();
        }
    }

    private void SaveSettings()
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        settings["spots"] = Spots;
    }

    // Code to execute when the application is launching (eg, from Start)
    // This code will not execute when the application is reactivated
    private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        LoadSettings();
    }

    // Code to execute when the application is activated (brought to foreground)
    // This code will not execute when the application is first launched
    private void Application_Activated(object sender, ActivatedEventArgs e)
    {
        LoadSettings();
    }

    // Code to execute when the application is deactivated (sent to background)
    // This code will not execute when the application is closing
    private void Application_Deactivated(object sender, DeactivatedEventArgs e)
    {
        SaveSettings();
    }

    // Code to execute when the application is closing (eg, user hit Back)
    // This code will not execute when the application is deactivated
    private void Application_Closing(object sender, ClosingEventArgs e)
    {
        SaveSettings();
    }

Seems straightforward enough. I set breakpoints in all these methods. When I hit F5 to deploy the app, the event handlers that are hit are:

Application_Launching() Application_Deactivated()

Oddly, these are hit, even though the emulator doesn't show the app opening or closing.

In the emulator, I then open the app, play around, close it, then re-open it. I use both the "back" and "start" buttons to close it. Despite this, I am unable to get any event handlers to be hit again.

What am I doing wrong here?

+2  A: 

Is the debug session is still active?

I have found that if you set breakpoints that get hit on start-up and you do not continue with a certain amount of time (e.g. < 10 seconds) your debug session will be disconnected.as the OS terminates the application.

Dennis Roche
+2  A: 

Like Dennis said, to debug the Activated event handler you need to launch a new debug session just after pressing the back button. The sequence would be :

  • launch debug
  • play with the app, hit Start
  • quit application, debug session stopped
  • hit back button - black screen on the emulator
  • launch debug session , be quick :) after 10 sec, OS terminates the application
Matthieu
Thanks, but this code still isn't working. (I save a collection with 9 elements, and get one with 0 back out.) Do you know why that could be, or should I make a new question?
Rosarch
Can we see some code of your handlers (desactived/actived) in the question ?
Matthieu
It seems that the emulator reset the IsolatedStorage system when closing it : http://www.codebadger.com/blog/post/2010/09/03/Using-Isolated-Storage-on-Windows-Phone-7.aspx (end of the document). Did you try to save your state into PhoneApplicationService.Current.State ?
Matthieu