如何在C#Xamarin中序列化异步
我正在使用JSON编写用户输入的数据。我希望使用serializeasync
来完成此操作。但是,如何根据此代码最好地做到这一点?
这是我的代码,没有serializySync
public static bool TempFileExists()
{
return File.Exists(TempJsonPath());
}
public static async Task WriteToTempJsonAsync(DeclarationJoined declaration)
{
if (TempFileExists())
{
var json = JsonSerializer.Serialize(declaration);
File.WriteAllText(PaymentCardListPath(), json);
}
}
,这就是我
public static async Task WriteToTempJsonAsync(DeclarationJoined declaration)
{
if (TempFileExists())
{
Stream writer = new FileStream(TempJsonPath(), FileMode.Append);
await JsonSerializer.SerializeAsync(writer, declaration);
}
}
每次执行此操作serializeasync
时都尝试过的。我该如何异步执行此操作?
I'm using json to write data that the user enters. I want this to be done with SerializeAsync
. But how can I best do this based on this code?
This is my code without SerializeAsync
public static bool TempFileExists()
{
return File.Exists(TempJsonPath());
}
public static async Task WriteToTempJsonAsync(DeclarationJoined declaration)
{
if (TempFileExists())
{
var json = JsonSerializer.Serialize(declaration);
File.WriteAllText(PaymentCardListPath(), json);
}
}
And this is what I have tried
public static async Task WriteToTempJsonAsync(DeclarationJoined declaration)
{
if (TempFileExists())
{
Stream writer = new FileStream(TempJsonPath(), FileMode.Append);
await JsonSerializer.SerializeAsync(writer, declaration);
}
}
Every time I do this SerializeAsync
I got a sharing violation. How can I do this asynchronously?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论