private void b_addFoodDialogAccept_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(tb_newFoodTextBox.Text)) { lb_searchResults.ItemsSource = null; return; } var newFood = new Model.Food(); newFood.name = tb_newFoodTextBox.Text; newFood.nutritionValuesString = temporaryNutritionValuesString; if (temporaryFood != null) { newFood.calories = APIRootObjectToNutritionValuesStringConverter.extractCaloriesFromAPIRootObject(temporaryFood); } else { newFood.calories = 0; } App._vmData.foodList.Add(newFood); temporaryNutritionValuesString = ""; temporaryFood = null; FoodCalculation.calculateFood(); }
private void lb_searchResults_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (lb_searchResults.SelectedItem == null) { return; } dontUpdate = true; pb_AddFoodLoadingBar.Visibility = Visibility.Visible; exp_temporaryNutritionValuesExpander.Visibility = Visibility.Visible; tb_newFoodTextBox.Text = lb_searchResults.SelectedItem.ToString(); //check if this gets fired from here //Wait for UI Thread to display Loading Bar Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => { })).Wait(); BackgroundWorker bw = new BackgroundWorker(); // what to do in the background thread bw.DoWork += new DoWorkEventHandler( delegate(object o, DoWorkEventArgs args) { BackgroundWorker b = o as BackgroundWorker; //Set the result and do the API Request args.Result = RequestFoodData.getNutritionValuesForSpecificFoodItemCommon(args.Argument as string); }); // what to do when worker completes its task (notify the user) bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler( delegate(object o, RunWorkerCompletedEventArgs args) { temporaryFood = args.Result as APIRootObject; temporaryNutritionValuesString = APIRootObjectToNutritionValuesStringConverter.createStringFromAPIRootObject(temporaryFood); tb_temporaryNutritionValuesTextBox.Text = temporaryNutritionValuesString; pb_AddFoodLoadingBar.Visibility = Visibility.Hidden; dontUpdate = false; }); var searchString = tb_newFoodTextBox.Text; bw.RunWorkerAsync(searchString); }