0

我目前正在为 Windows Phone 7.1 开发应用程序,并且需要在用户退出应用程序时保存一些数据。

该应用程序非常简单:MainPage 是用户看到的第一件事,他们在其中选择了四个购物中心之一。下一页询问他们将车停在哪里并将其存储为字符串变量。最后一页加载该字符串变量并向用户显示相关信息,以及自启动应用程序以来一直在运行的计时器。

我要保存的是用户离开应用程序时的用户输入数据和计时器值,以便再次启动它时,它会自动显示包含用户信息的最后一页。

我一直在玩生成的 Application_Launching、Activated 等事件,但到目前为止还没有工作。任何帮助将不胜感激。

编辑:这是我到目前为止的一些代码(没有带我去任何地方)

    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);
        }
    }

那在我的 App.xaml.cs 类中

4

1 回答 1

0

您必须查看 app.cs 文件中的事件,但这也取决于您在应用程序被激活、停用、关闭和启动时想要什么。

    // 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();
    }
于 2013-01-29T21:05:59.503 回答