/// <summary> /// Display results for selected food item. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnDetails_Clicked(System.Object sender, System.EventArgs e) { //Validate user has selected a picker option. if (PckFood.SelectedIndex > -1) { //Adds a new instance of a food item. FoodItems selectedFood = new FoodItems(); //Looks at each individual item within the foodList. foreach (FoodItems food in foodList) { //Check if the food item equals the selected item from the picker. if (food.FoodName == PckFood.SelectedItem.ToString()) { //The new instance of the food (selectedFood) equals the one found in the list. selectedFood = food; } } //Show users results for selected food item with items calegory, measurement, and calories information in a display alert. DisplayAlert(selectedFood.FoodName, $"Category: {selectedFood.Category}\n\n" + $"Measurement: {selectedFood.Measurement}\n\n" + $"Calories: {selectedFood.Calories.ToString("n0")}", "Close"); } else { //User hasn't selected an item, show alert to telling them to select one. DisplayAlert("Invalid Input", "Please select a food item first.", "Close"); } }
/// <summary> /// Processes the data file and add food items to picker. /// </summary> private void LoadFoodFile() { //Allows access to the saved imbedded text file in the solution explorer assembly. var assembly = IntrospectionExtensions.GetTypeInfo(typeof(FoodDetailsPage)).Assembly; //Use stream to open up and find text file within the solution explorer. Stream stream = assembly.GetManifestResourceStream("MMFitnessApp.food.txt"); //String array to store each of the food items from the text file. string[] foodItem; //List of string to hold food names to populate picker. List <string> foodNames = new List <string>(); //Process the food text file. Allows to read each line in the text file. using (var reader = new StreamReader(stream)) { //Skips the headers in the food text file. reader.ReadLine(); //To loop through each line and store it in the string array. Read until not at the end of the stream. while (!reader.EndOfStream) { //Read file line for line. Array after each tab. foodItem = reader.ReadLine().Split('\t'); //Create a new food with each loop. FoodItems food = new FoodItems(foodItem); //Adding a food name from the foodItems. foodNames.Add(food.FoodName); //Provides a food for the list. foodList.Add(food); } ; } //Adds the names to the picker PckFood.ItemsSource = foodNames; }