我正在尝试在我的应用程序中保存动漫词典,其中包含字符串作为键和动画作为值,但是当我使用保存功能时,应用程序只会被卡住而无异常
这是我的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() { 等待Windows.System.Threading.ThreadPool.RunAsync((sender)=&gt; { Sys.SaveAsync()等待(); },Windows.System.Threading.WorkItemPriority.Normal); }
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
答案 0 :(得分:0)
您的异常可能会被吞下,具体取决于它所引发的线程,请确保您已设置为在visual studio中抛出异常中断。您可以在调试菜单上设置&gt;例外&gt;在Thrown
Common Languange Runtime Exceptions
此外,您可以取消Save<T>
/ Restore<T>
方法并直接致电SaveAsync<T>
/ RestoreAsync<T>
,无论如何它们都是异步的Save<T>
/ {{ 1}}有点多余。它还会对线程池创建一个不必要的调用。