我正在使用XmlSerializer,每次将项目添加到我的列表中时我都会调用save方法:
async public void save(List<string> eingabe)
{
var serializer = new XmlSerializer(typeof(List<string>));
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile =
await storageFolder.CreateFileAsync(speicherPfad, CreationCollisionOption.ReplaceExisting);
var file = await sampleFile.OpenAsync(FileAccessMode.ReadWrite);
serializer.Serialize(file.AsStreamForWrite(), eingabe);
file.Dispose();
}
当应用程序启动时,我调用load方法:
public async Task<List<string>> load()
{
List<string> ausgabe = new List<string>();
XmlSerializer serializer = new XmlSerializer(typeof(List<string>));
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.CreateFileAsync(speicherPfad, CreationCollisionOption.ReplaceExisting);
var file = await sampleFile.OpenAsync(FileAccessMode.Read);
List<string> speicher = (List<string>)serializer.Deserialize(file.AsStreamForRead());
file.Dispose();
return speicher;
}
问题:在加载方法中,文件总是空的(大小= 0),我不知道为什么
答案 0 :(得分:1)
可能是因为这个选项:CreationCollisionOption.ReplaceExisting
。
作为旁注,最好将save
更改为返回Task
和await
。