删除Windows Phone C#app中的数组项,并且永远不会在下次应用启动时显示它

时间:2014-07-16 18:37:44

标签: c# arrays windows-phone-8 windows-phone

我正在使用C#开发Windows Phone应用程序。它在数组中有10000个元素。我的程序sudo代码类似于

Begin
Get a random element from array
Manipulate it
Delete it
End

应该从应用程序中透彻地删除该数组元素(即,我不应该在下次应用程序启动时获取它)

如何轻松执行此任务。请给我一些代码,以便我能够轻松理解。

2 个答案:

答案 0 :(得分:1)

您必须将数据存储在独立存储中或使用

SQL Compact Fow Windows Phone

答案 1 :(得分:1)

使用带有xml输出/输入的存储文件夹的基本示例。您可以按照自己的意愿修改它。我为自己的Windows Phone应用程序使用了更复杂的版本。

我假设您很难写回读数据。如果您需要帮助从列表中删除随机元素,请告诉我。我也会编辑这段代码。

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