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 是附加多个对象赖特?
怎么存放呢?以及如何检索它?