0

如何将数组列表存储到隔离存储中?也可以将图像放在数组列表中吗?谢谢

4

1 回答 1

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;
    }
}
}
于 2012-06-06T23:01:34.650 回答