Deleting on array item in Windows Phone C# app and

2019-03-07 19:00发布

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.

2条回答
Bombasti
2楼-- · 2019-03-07 19:12

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();            
   }
}
查看更多
家丑人穷心不美
3楼-- · 2019-03-07 19:26

You have to store your data in an Isolated Storage or use

SQL Compact Fow Windows Phone

查看更多
登录 后发表回答