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 fileNa me)
{
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);
}
}
}
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
await AskUserToLocalDataAsync();
if (useLocalData)
{
SomethingGoesHere = await AppDataManager.RestoreAsync<UserData>(fileName);
}
}
代碼工作正常,但它只返回一個對象,我想寫入多個對象並將其作爲observableCollection檢索以將其綁定到GridView。 GenerateUniqueName是追加多個對象賴特?序列化多個對象
如何存儲它?以及如何檢索它?
不要吞下例外 – SLaks