0
[DataContract]
class UserData
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public int phoneNumber { get; set; }

    [DataMember]
    public string Course { get; set; }
    [DataMember]
    public Uri Image { get; set; }
}

[DataContract]
class UserItems
{
    [DataMember]
    static ObservableCollection<UserData> _items = new ObservableCollection<UserData>();
    [DataMember]
    public static ObservableCollection<UserData> Items
    {
        get { return _items; }
    }

这就是AppDataManager课堂

class AppDataManager
{
    public static async Task SaveAsync<T>(T data, string fileName)
    {

        StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, 
            CreationCollisionOption.GenerateUniqueName);


        var stream = await file.OpenStreamForWriteAsync();
        var serializer = new DataContractSerializer(typeof(T));

        serializer.WriteObject(stream ,data);
        await stream.FlushAsync();
    }

    public static async Task<T> RestoreAsync<T>(string fileName)
    {
        try
        {
            var file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);

            var instream = await file.OpenStreamForReadAsync();
            var serializer = new DataContractSerializer(typeof(T));
            return (T)serializer.ReadObject(instream);
        }
        catch (Exception)
        {
            return default(T);
        }
    }
}
}

我试图保存一个对象并且它起作用了,但是集合是我的问题,我正在使用的应用程序是在他每次输入数据时保存用户数据。

问题是:如何保存数据集合以及如何将其作为数据集合检索?

4

1 回答 1

0

检查此链接,它是关于 Windows Phone 环境的,但我认为它也适用于您的情况: http: //geekswithblogs.net/mikebmcl/archive/2010/12/29/datacontractserializer-type-is-not-serializable-because-它不是公共的.aspx。使用属性装饰您的_items私人成员DataMember可能是问题所在。

如果不是这样,可能ObservableCollection<T>与.DataContractSerializerList<T>DataMember

于 2013-01-19T17:32:34.407 回答