Question

I'm currently developing an app for Windows Phone 7.1, and need to save some data for when the user quits the app.

The app is pretty straightforward: the MainPage is the first thing the user sees, in which they select one of four shopping centers. The next page asks them where they have parked their car and stores it as a String variable. The last page loads that String variable and displays back to the user the relevant information, along with a timer that has been ongoing since launching the app.

What I want to save is the user-input data and the timer value when the user leaves the app, so that when launching it again, it automatically shows the last page with the user's info in it.

I've been playing around with the generated Application_Launching, Activated, etc events, but so far can't get something to work. Any help would be greatly appreciated.

Edit: Here's some code that I have so far (hasn't led me anywhere)

    void LoadSettings()
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        String mall;
        String level;
        String letter;
        String number;
        if (settings.TryGetValue<String>("mall", out mall))
        {
            _mall = mall;
        }
        if (settings.TryGetValue<String>("level", out level))
        {
            _level = level;
        }
        if (settings.TryGetValue<String>("mall", out letter))
        {
            _letter = letter;
        }
        if (settings.TryGetValue<String>("mall", out number))
        {
            _number = number;
        }
    }

    void SaveSettings()
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        if (_mall != null)
        {
            settings["mall"] = (_mall as String);
            settings["level"] = (_level as String);
            settings["letter"] = (_letter as String);
            settings["number"] = (_number as String);
        }
    }

That's in my App.xaml.cs class

Was it helpful?

Solution

You have to look at the events in your app.cs file, but it also depends on what you want when tha application is Activated, Deactived, Closed and launched.

    // 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)
    {
        if (e.IsApplicationInstancePreserved)
        {
            //The application was dormant and state was automatically preserved
        }
        else
        {
            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();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top