在IsolatedStorage中存储arraylist

时间:2012-06-06 17:41:08

标签: c# windows-phone-7 windows-phone

如何将数组列表存储到独立存储中?也可以将图像放在arraylist中?谢谢

1 个答案:

答案 0 :(得分:1)

就像评论所说的,所有你需要的是获得一些可序列化的对象,你就可以将它存储在IS中。请注意,多个维度的数组不可序列化!

这是我用于IS的代码块:

using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;

namespace PhoneApp1
{
public class IsolatedStorage
{
    public static void SaveToIs(String fileName, Object saved)
    {
        try
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isf.FileExists(fileName))
                {
                    isf.DeleteFile(fileName);
                }


                using (IsolatedStorageFileStream fs = isf.CreateFile(fileName))
                {

                    XmlSerializer ser = new XmlSerializer(saved.GetType());
                    ser.Serialize(fs, saved);
                }
            }
        }
        catch (IsolatedStorageException ex)
        {
            MessageBox.Show(ex.Message);
        }


    }

    public static Object loadFromIS(String fileName, Type t)
    {
        Object result = null;
        try
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isf.FileExists(fileName))
                {

                    using (StreamReader sr = new StreamReader(isf.OpenFile(fileName, FileMode.Open)))
                    {
                        XmlSerializer ser = new XmlSerializer(t);
                        result = ser.Deserialize(sr);
                    }
                }
            }
        }
        catch (IsolatedStorageException ex)
        {
            MessageBox.Show(ex.Message);
        }
        catch (InvalidOperationException e)
        {
            MessageBox.Show(e.Message);
        }
        return result;
    }
}
}