问题描述
我正在使用 C# 开发 Windows Phone 应用程序.它在一个数组中有 10000 个元素.我的程序 sudo 代码类似于
I am developing a Windows Phone app with C#. It has 10000 elements in an array. My program sudo code is something like
Begin
Get a random element from array
Manipulate it
Delete it
End
并且该数组元素应该从应用程序中永久删除,(即,我不应该在下次应用程序启动时获取它)
And that array element should be permenetly deleted from the app, (ie, I should not get it on next app launch)
如何轻松执行此任务.请给我一些代码,以便我容易理解.
How to perform this task easily. Please give me some code so I can understand easily.
推荐答案
使用带有 xml 输出/输入的存储文件夹的基本示例.你可以修改它做你想做的.我为自己的 Windows 手机应用程序使用了更复杂的版本.
Pretty basic example of using storage folder with xml output/input. You can modify it do what you wish. I use a more complicated version of it for my own windows phone app.
我假设您很难写入和读取数据.如果您需要帮助从列表中删除随机元素,请告诉我.我也会为此编辑此代码.
I'm assuming you having a hard time writing and reading the data back. If you need help deleting a random element from the list, let me know. I will edit this code for that as well.
private List<int> my_list = new List<int>();
public async Task GenericDataWrite()
{
// Get the local folder.
StorageFolder data_folder = Windows.Storage.ApplicationData.Current.LocalFolder;
// Create a new file named data_file.xml
StorageFile file = await data_folder.CreateFileAsync(@"data_file.xml", CreationCollisionOption.ReplaceExisting);
// Write the data
using (Stream s = await file.OpenStreamForWriteAsync())
{
try
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<int>));
serializer.Serialize(s, my_list);
s.Close();
}
catch(Exception ex)
{
string error_message = ex.Message;
}
}
}
public async Task GenericDataRead()
{
// Get the local folder.
StorageFolder data_folder = Windows.Storage.ApplicationData.Current.LocalFolder;
if (data_folder != null)
{
StorageFile file = await data_folder.GetFileAsync(@"data_file.xml");
// Get the file.
System.IO.Stream file_stream = await file.OpenStreamForReadAsync();
// Read the data.
using (StreamReader streamReader = new StreamReader(file_stream))
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<int>));
my_list = (List<int>)serializer.Deserialize(streamReader);
streamReader.Close();
}
file_stream.Close();
}
}
这篇关于删除 Windows Phone C# 应用程序中的数组项,并在下次应用程序启动时不再显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!