private async Task GatherResults(ISurveyQuestionService service, SurveyQuestion question) { string[] answers = question.Answers.Split('|'); var responses = await service.GetResponsesForSurveyAsync(question.Id); foreach (var r in responses) { Results.Add(new OneResultViewModel { Name = r.Name, Answer = answers [r.ResponseIndex] }); } }
private async void OnShowResults(object sender, EventArgs e) { var selectedQuestion = GetSelectedQuestion(); if (selectedQuestion == null) { return; } string[] answers = selectedQuestion.Answers.Split('|'); // Populate the results asynchronously -- do not await this call or // it will become synchronous with respect to the push to the next screen. var results = new ObservableCollection <Tuple <string, string> >(); var fillDataTask = service.GetResponsesForSurveyAsync(selectedQuestion.Id); var t1 = fillDataTask.ContinueWith(tr => { foreach (var r in tr.Result) { if (r.ResponseIndex >= 0 && r.ResponseIndex < answers.Length) { results.Add(Tuple.Create(r.Name, answers[r.ResponseIndex])); } } }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext()); // If our async fill fails, then show an error. var t2 = fillDataTask.ContinueWith(async tr => { await this.DisplayAlert("Error", "Failed to download response: " + tr.Exception.Message, "OK"); }, TaskContinuationOptions.OnlyOnFaulted); // And display the results page. await Navigation.PushAsync(new ResultsPage { BindingContext = new { Question = selectedQuestion.Question, Answers = results } }); }