创建多个文本列表并将其保存到独立存储中
如何创建多个文本数据列表并将其保存到独立存储中? 我还需要检索并显示不同的已保存列表。
我正在做一个像饮料清单这样的应用程序,用户可以创建包含许多不同种类饮料的多个饮料清单。
我目前只能创建并保存一份饮料文本列表。如果我再次在列表中添加更多饮料文本并保存,则列表将被最新的不同饮料文本覆盖。
//保存饮料列表文本
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将其另存为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.
您的 addListBtn_Click 方法假设它可以在您的应用程序实例的 userDrinksList 成员中找到饮料列表,但是您的 PhoneApplicationPage_Loaded 方法不会填充该成员。
在您的 PhoneApplicationPage_Loaded 方法中,您可以执行以下操作:
“使用”确保资源正确关闭/处置,因此您不需要显式关闭。您正在阅读完整的内容 - 您需要逐行阅读。
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:
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.