有没有一种方法可以将Windows.Storage.ApplicationData与UWP应用程序中的foreach循环一起使用来保存列表?

时间:2019-08-12 17:34:56

标签: c# uwp

我正在制作一个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);
                }
            }
        }

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

        }

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

1 个答案:

答案 0 :(得分:0)

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

从代码中可以看到,您的foodDatabaseListList<string>,我们将从这里开始。

在此之前,您需要安装 Newtonsoft.Json nuget软件包。

StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile databaseFile = await storageFolder.CreateFileAsync("database.txt", CreationCollisionOption.ReplaceExisting);

// Data loading

// List serialization
string serialization = JsonConvert.SerializeObject(foodDatabaseList);
// Save them
await FileIO.WriteTextAsync(databaseFile, serialization);

如果您需要阅读它们

Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile databaseFile = await storageFolder.CreateFileAsync("database.txt", CreationCollisionOption.OpenIfExists);

string test = await FileIO.ReadTextAsync(databaseFile);

List<string> localItems = JsonConvert.DeserializeObject<List<string>>(test);

foreach (var item in localItems)
{
    lstFoodDatabase.Items.Add(item);
}

最诚挚的问候。