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);
}
}
}
}
我想救一個對象和它的工作,但收藏是我的問題,應用程序IM與合作每次輸入數據時都要保存用戶數據。
現在的問題是:如何保存數據收集和如何檢索它作爲一個數據集合?
這篇文章可能有所幫助:http://stackoverflow.com/questions/13923600/how-to-save-observablecollection-in-windows-store-app –
UserItems._items是私有的,UserItems.Items沒有設置器。最好只將該屬性標記爲DataMember並給它一個setter。如果它只是RestoreAsync失敗,那麼你有你的答案 – kindasimple