如何在C#Xamarin中序列化异步

发布于 2025-02-10 01:25:58 字数 849 浏览 0 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文