简体   繁体   English

下次启动应用程序时,如何保存应用程序数据以重新加载? WP8.1

[英]How to save App Data to reload it when app starts next time? WP8.1

Let's say there's a Textbox in my app and I'm getting the username from the Textbox. 假设我的应用程序中有一个文本框,而我从文本框获取用户名。 I'm saving the username to a string. 我将用户名保存为字符串。 But when I relaunch the app, the string will be null. 但是,当我重新启动该应用程序时,该字符串将为null。 How can I save user data so that I will get it next time my app starts? 如何保存用户数据,以便下次启动我的应用程序时能获得它?

I know File reading & writing methods but I'm looking for a way that will not involve file reading & writing. 我知道文件读写方法,但是我正在寻找一种不涉及文件读写的方法。

I'd use project settings ( Settings Page, Project Designer ). 我会使用项目设置( “设置页”,“项目设计器” )。 Once a setting is created you can set from the GUI like this: 创建设置后,您可以通过GUI进行如下设置:

Properties.Settings.Default.FirstUserSetting = "abc";

When you close the main form (there is an event for this) you can persist all settings like this: 关闭主窗体时(有一个事件发生),您可以像这样保留所有设置:

Properties.Settings.Default.Save();

Ultimately a file in the user's profile is used to store the data, but you don't have to deal with it directly. 最终,用户配置文件中的文件用于存储数据,但是您不必直接处理它。

If you need to save a list of, for example strings, you can do this: 如果您需要保存例如字符串的列表,则可以执行以下操作:

private void Form1_Load(object sender, EventArgs e)
{
    string[] someList = Properties.Settings.Default.myList.Split('|');
    listBox1.Items.AddRange(someList);
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    string[] someList = new string[listBox1.Items.Count];
    for (int i = 0; i < listBox1.Items.Count; i++)
    {
        someList[i] = (string)listBox1.Items[i];
    }
    Properties.Settings.Default.myList = string.Join("|", someList);
    Properties.Settings.Default.Save();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM