首页 > 解决方案 > 输入字符串的格式不正确 - 使用数据库时

问题描述

我正在使用 c# 中的 xamarin,它使用一个包含用户列表的数据库,每个用户都有一个他们的最高分数列表。我正在尝试使用新分数更新分数列表,如下所示:

public Score GetScore(DatabaseReference reference)
        {
            Score score = new Score();
            score.time = TimeSpan.ParseExact(reference.Child("Time").ToString(), @"hh\:mm\:ss\.fffffff", CultureInfo.InvariantCulture);
            score.rightAnswers = int.Parse(reference.Child("Right Answers").ToString());
            score.genre = reference.Child("Genre").ToString();
            return score;
        }

        public async Task UpdateActivity(string username, Score score)
        {
            User user = await GetUser(username);
            DatabaseReference reference = GetDatabase().GetReference("Users").Child(user.userId);
            if (score != null)
            {
                if (await CheckIfScoreExists(username, score) == true)
                {
                    string id = await GetExistingScoreId(username, score);
                    DatabaseReference scoreRef = reference.Child("Scores").Child(id);
                    Score existingScore = GetScore(scoreRef);
                    if (score.IsHigher(existingScore))
                    {
                        reference.Child(id).RemoveValue();
                        HashMap newScore = new HashMap();
                        newScore.Put("Genre", score.genre);
                        newScore.Put("Time", score.time.ToString());
                        newScore.Put("Right Answers", score.rightAnswers);
                        reference.Child("Scores").Push().SetValue(newScore);
                    }

                }
                else
                {
                    HashMap newScore = new HashMap();
                    newScore.Put("Genre", score.genre);
                    newScore.Put("Time", score.time.ToString());
                    newScore.Put("Right Answers", score.rightAnswers);
                    reference.Child("Scores").Push().SetValue(newScore);
                }
                
            }

但是当我运行应用程序并尝试更新它时,它说“输入字符串的格式不正确。” 关于 rightAnswers 属性。我该如何解决?

注意:函数 GetExistingScoreId 是在给定乐谱的类型中的乐谱已经存在的情况下,因此我可以在需要时替换现有乐谱。

标签: c#androiddatabasexamarin

解决方案


推荐阅读