我正在尝试将动漫字典保存在我的应用程序中,其中包含字符串作为键和动漫作为值但是当我使用保存功能时,应用程序只是卡住了,没有例外
这是我的 Anime.cs 课程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace AnimeTrackerNew
{
[KnownType(typeof(AnimeTrackerNew.Anime))]
[DataContractAttribute]
class Anime:IComparable<Anime>
{
private string name;
private int season;
private int noe;
private int lw;
private Boolean finished;
public Anime(string name)
: base()
{
this.name = name;
}
public Anime(string name,int season, int noe, int lw)
: base()
{
this.name = name;
this.season = season;
this.noe = noe;
this.lw = lw;
if (lw == noe)
finished=true;
else
finished=false;
}
[DataMember]
public string Name
{
get
{
return name;
}
set
{
this.name = value;
}
}
[DataMember]
public int Season
{
get
{
return season;
}
set
{
this.season = value;
}
}
[DataMember]
public int Noe
{
get
{
return noe;
}
set
{
this.noe = value;
}
}
[DataMember]
public int Lw
{
get
{
return lw;
}
set
{
this.lw = value;
}
}
[DataMember]
public Boolean Finished
{
get
{
return finished;
}
set
{
this.finished = value;
}
}
public int CompareTo(Anime other)
{
return this.Name.CompareTo(other.Name);
}
}
}
这些是保存函数 static async public Task Save() { await Windows.System.Threading.ThreadPool.RunAsync((sender) => { Sys.SaveAsync().Wait(); }, Windows.System.Threading.WorkItemPriority 。普通的); }
static async public Task Restore<T>()
{
await Windows.System.Threading.ThreadPool.RunAsync((sender) =>
{
Sys.RestoreAsync<T>().Wait();
}, Windows.System.Threading.WorkItemPriority.Normal);
}
static async private Task SaveAsync<T>()
{
StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("animes.xml", CreationCollisionOption.ReplaceExisting);
IRandomAccessStream sessionRandomAccess = await sessionFile.OpenAsync(FileAccessMode.ReadWrite);
IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary<string,Anime>), new Type[] { typeof(T) });
sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), animes);
await sessionOutputStream.FlushAsync();
sessionRandomAccess.Dispose();
}
static async private Task RestoreAsync<T>()
{
StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("animes.xml", CreationCollisionOption.OpenIfExists);
if (sessionFile == null)
{
return;
}
IInputStream sessionInputStream = await sessionFile.OpenReadAsync();
DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary<string,Anime>), new Type[] { typeof(T) });
animes = (Dictionary<string, Anime>)sessionSerializer.ReadObject(sessionInputStream.AsStreamForRead());
}
}
}
经过一些调试后,该应用程序似乎卡在了这条线上
await sessionOutputStream.FlushAsync();
对此有任何帮助都会得到帮助:D