[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; }
}
and this is the AppDataManager
class
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);
}
}
}
}
I tried to save one object and it worked, but the collection is my problem, the app im working with is to save the user data every time he entered the data.
The question is : How to save the collection of data and how to retrieve it as a collection of data?