Пример #1
0
        // private (asynchronous) helper methods
        private async Task Insert(String name, int score)
        {
            // insert name and score into local database
            await this.service.InsertIntoLocalTable(name, score);

            // update observable collection bind to UI
            HighScoreEntry next = new HighScoreEntry()
            {
                Name     = name,
                Score    = score,
                Position = 1
            };

            if (this.highScorers.Count == 0)
            {
                // empty collection
                this.highScorers.Add(next);
            }
            else
            {
                // seach position in observable collection, where to insert
                int i = this.highScorers.Count;
                while (i > 0)
                {
                    HighScoreEntry entry = this.highScorers[i - 1];
                    if (score > entry.Score)
                    {
                        i--;
                    }
                    else
                    {
                        break;
                    }
                }

                // insert new entry
                this.highScorers.Insert(i, next);
            }

            // adjust property 'Position' of all entries
            for (int i = 0; i < this.highScorers.Count; i++)
            {
                HighScoreEntry entry = this.highScorers[i];
                entry.Position = i + 1;
            }
        }
Пример #2
0
        private async Task Sync()
        {
            // retrieve actual list of high scores from backend
            List <FirstHighScores> currentHighScores = await this.service.Sync();

            this.highScorers.Clear();

            // map list into observable collection (without AutoMapper :-( )
            for (int i = 0; i < currentHighScores.Count; i++)
            {
                HighScoreEntry entry = new HighScoreEntry()
                {
                    Name     = currentHighScores[i].Name,
                    Score    = currentHighScores[i].Score,
                    Position = i + 1
                };

                this.highScorers.Add(entry);
            }
        }