[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);
}
}
}
}
我试图保存一个对象并且它起作用了,但是集合是我的问题,我正在使用的应用程序是在他每次输入数据时保存用户数据。
问题是:如何保存数据集合以及如何将其作为数据集合检索?