C# 有没有办法在UWP应用程序中使用Windows.Storage.ApplicationData和foreach循环保存列表?

C# 有没有办法在UWP应用程序中使用Windows.Storage.ApplicationData和foreach循环保存列表?,c#,uwp,C#,Uwp,我正在制作一个UWP应用程序,当用户关闭应用程序时,我想在其中保存列表框的内容。我相信(尽管我可能错了)最好的方法是将列表框的内容添加到列表中。然而,考虑到UWP的限制,我不能使用StreamWriter来保存我希望保存的任何变量,这将使我能够轻松地保存这些变量。因此,我使用的是StorageFolder.GetFileAsync 虽然这对于保存一些变量来说是很好的,但当我想要保存的变量数量不是预先确定的(即列表)时,我不确定如何进行保存 private async void btnAdd_Cl

我正在制作一个UWP应用程序,当用户关闭应用程序时,我想在其中保存列表框的内容。我相信(尽管我可能错了)最好的方法是将列表框的内容添加到列表中。然而,考虑到UWP的限制,我不能使用
StreamWriter
来保存我希望保存的任何变量,这将使我能够轻松地保存这些变量。因此,我使用的是
StorageFolder.GetFileAsync

虽然这对于保存一些变量来说是很好的,但当我想要保存的变量数量不是预先确定的(即列表)时,我不确定如何进行保存

private async void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            Windows.Storage.StorageFolder storageFolder = 
            Windows.Storage.ApplicationData.Current.LocalFolder;
            Windows.Storage.StorageFile databaseFile = await 
            storageFolder.CreateFileAsync("database.txt", 
            Windows.Storage.CreationCollisionOption.ReplaceExisting);

            string databaseItem;
            string databaseName;
            double databaseFat;
            double databaseCarbs;
            double databaseProtein;
            string servingSize;

            txtNameDatabase.Text = txtCaloriesDatabase.Text = txtFatDatabase.Text = txtCarbsDatabase.Text = txtProteinDatabase.Text = txtServingSize.Text = "";
            int databaseCalories;

            ContentDialogResult databaseResult = await databaseContentDialog.ShowAsync();
            bool contentDialogError = false;

            if (databaseResult == ContentDialogResult.Primary)
            {
                try
                {
                    databaseName = txtNameDatabase.Text;
                    databaseCalories = Convert.ToInt32(txtCaloriesDatabase.Text);
                    databaseFat = Convert.ToDouble(txtFatDatabase.Text);
                    databaseCarbs = Convert.ToDouble(txtCarbsDatabase.Text);
                    databaseProtein = Convert.ToDouble(txtProteinDatabase.Text);
                    servingSize = txtServingSize.Text;


                    if (contentDialogError == false)
                    {

                        databaseItem = databaseName + ": " + databaseCalories + " Calories, " + databaseFat + "g Fat, " + databaseCarbs + "g Carbs, " + databaseProtein + "g Protein, Serving Size: " + databaseProtein;
                        foodDatabaseList.Add(databaseItem);
                        lstFoodDatabase.Items.Add(foodDatabaseList[0]);
                    }
                }
                catch
                {
                    var msgContentDialogError = new MessageDialog("Please only enter whole numbers into the calories field and decimal numbers into the macronutrient fields.");
                    await msgContentDialogError.ShowAsync();
                }
                foreach (string line in lstFoodDatabase.Items)
                {
                    await Windows.Storage.FileIO.WriteTextAsync(databaseFile, line);
                }
            }
        }


我尝试使用
foreach
循环来保存列表框中的每个项目,但是当我尝试读取它时,我只得到最后保存的项目。

当您想要保存一些结构化数据时,通常有两种方法。
第一个是
数据库
,但此方法可能不适合您的情况。
第二种方法是将数据存储为可序列化文本,例如
JSON

正如您从代码中看到的,您的
foodDatabaseList
是一个
列表
,我们将从这里开始

在此之前,您需要安装Newtonsoft.Jsonnuget包

StorageFolder-StorageFolder=ApplicationData.Current.LocalFolder;
StorageFile databaseFile=wait-storageFolder.CreateFileAsync(“database.txt”,CreationCollisionOption.ReplaceExisting);
//数据加载
//列表序列化
string serialization=JsonConvert.SerializationObject(foodDatabaseList);
//拯救他们
等待FileIO.WriteTextAsync(数据库文件,序列化);
如果你需要读的话

Windows.Storage.StorageFolder-StorageFolder=Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile databaseFile=wait-storageFolder.CreateFileAsync(“database.txt”,CreationCollisionOption.OpenIfExists);
字符串测试=等待文件IO.ReadTextAsync(数据库文件);
List localItems=JsonConvert.DeserializeObject(测试);
foreach(本地化项中的var项)
{
lstFoodDatabase.Items.Add(item);
}
致以最良好的祝愿

private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            Windows.Storage.StorageFile databaseFile = await storageFolder.GetFileAsync("database.txt");

            string test = await Windows.Storage.FileIO.ReadTextAsync(databaseFile);
            lstFoodDatabase.Items.Add(test);

        }