创建多个文本列表并将其保存到独立存储中

发布于 2024-11-06 06:37:23 字数 1449 浏览 0 评论 0原文

如何创建多个文本数据列表并将其保存到独立存储中? 我还需要检索并显示不同的已保存列表。

我正在做一个像饮料清单这样的应用程序,用户可以创建包含许多不同种类饮料的多个饮料清单。

我目前只能创建并保存一份饮料文本列表。如果我再次在列表中添加更多饮料文本并保存,则列表将被最新的不同饮料文本覆盖。


//保存饮料列表文本

    private void addListBtn_Click(object sender, RoutedEventArgs e)
    {

        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
        storage.CreateDirectory("ListFolder");

        StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("ListFolder\\savedList.txt", FileMode.OpenOrCreate, storage));
        for (int i = 0; i < (Application.Current as App).userDrinksList.Count; i++)
        {
            String drink = (Application.Current as App).userDrinksList[i].ToString();
            writeFile.WriteLine(drink.ToString());
        }
        writeFile.Close();

        MessageBox.Show("List added into favourite list.");
     }

//显示保存的列表

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();

        StreamReader readFile = null;
        {
            readFile = new StreamReader(new IsolatedStorageFileStream("ListFolder\\savedList.txt", FileMode.Open, storage));

            listNumberListBox.Items.Add(readFile.ReadToEnd());
            readFile.Close();
        }
    }

How do I create more than one list of text data and save it into the isolated storage?
I need to retrieve and display different saved list as well.

I am doing an application like a drink list where user can create multiple drink list containing many different kinds of drink.

I can only create and save one list of drink text at the moment. If I were to add more drink text inside the list again and save it, the list will be overwritten by the latest different drink text.


// Save List of drink text

    private void addListBtn_Click(object sender, RoutedEventArgs e)
    {

        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
        storage.CreateDirectory("ListFolder");

        StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("ListFolder\\savedList.txt", FileMode.OpenOrCreate, storage));
        for (int i = 0; i < (Application.Current as App).userDrinksList.Count; i++)
        {
            String drink = (Application.Current as App).userDrinksList[i].ToString();
            writeFile.WriteLine(drink.ToString());
        }
        writeFile.Close();

        MessageBox.Show("List added into favourite list.");
     }

// Display saved lists

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();

        StreamReader readFile = null;
        {
            readFile = new StreamReader(new IsolatedStorageFileStream("ListFolder\\savedList.txt", FileMode.Open, storage));

            listNumberListBox.Items.Add(readFile.ReadToEnd());
            readFile.Close();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久隐师 2024-11-13 06:37:23

您将其另存为savedList.txt。您需要将每个列表保存为单独的文件。例如list1.txt、list2.txt 等。

也许您还需要一个列表列表,以便您知道哪个文件=哪个列表。

You are saving it as savedList.txt. You need to save each list as a separate file. eg list1.txt, list2.txt etc.

Perhaps you also need a list of lists so you know which file = which list.

极度宠爱 2024-11-13 06:37:23

您的 addListBtn_Click 方法假设它可以在您的应用程序实例的 userDrinksList 成员中找到饮料列表,但是您的 PhoneApplicationPage_Loaded 方法不会填充该成员。

在您的 PhoneApplicationPage_Loaded 方法中,您可以执行以下操作:

    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    using(var stream = storage.OpenFile("ListFolder\\savedList.txt", FileMode.Open))
    using(StreamReader readFile = new StreamReader(stream))
    {
        for (string line = readFile.ReadLine(); line != null; line = readFile.ReadLine())
        {
            listNumberListBox.Items.Add(line);
            ((App) Application.Current).userDrinksList.Add(line)
        }
    }

“使用”确保资源正确关闭/处置,因此您不需要显式关闭。您正在阅读完整的内容 - 您需要逐行阅读。

Your addListBtn_Click method is assuming it can find the list of drinks in a userDrinksList member of your Application instance, however your PhoneApplicationPage_Loaded method doesn't populate that member.

In your PhoneApplicationPage_Loaded method you could do:

    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    using(var stream = storage.OpenFile("ListFolder\\savedList.txt", FileMode.Open))
    using(StreamReader readFile = new StreamReader(stream))
    {
        for (string line = readFile.ReadLine(); line != null; line = readFile.ReadLine())
        {
            listNumberListBox.Items.Add(line);
            ((App) Application.Current).userDrinksList.Add(line)
        }
    }

The 'usings' ensure that the resources are properly closed/disposed, so you don't need to explicitly close. You were reading in the complete contents - you need to read it in line by line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文