0

I am trying to to use an AutoSuggestBox to Display the Data as being typed from a string.

I have tried making an array with some words that will be shown in the AutoSuggestBox as the user types, I however need the Data from the API which is being stored in a string to show in the AutoSuggestBox. For now, I am using an array to show 3 words as the user types, and the string that contains the API data, being added into a ListBox.

This however is being done in the AutoSuggestBox_TextChanged method, so as the user types, the data that we get gets added to the ListBox.

private async void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
        {
            string[] Autoitems = new string[] { "check", "apple", "banana" } //Temporary Array for AutoSuggestBox

            var Auto = (AutoSuggestBox)sender;
            var Suggestion = Autoitems.Where(p => p.StartsWith(Auto.Text, StringComparison.OrdinalIgnoreCase)).ToArray();
            Auto.ItemsSource = Suggestion; //This displays only items from array as being typed.

            string searchedName = SearchBox.Text;

            myFood = await NutritionixAPI.GetFood(searchedName);

            //The data I get from the API is stored in the temp string
            string temp = myFood.hits[0].fields.item_name + " Calories: " + myFood.hits[0].fields.nf_calories + " Protein: " + myFood.hits[0].fields.nf_protein + " Fat: " + myFood.hits[0].fields.nf_total_fat;
            ResultListBox.Items.Add(temp); //temp string data is added to a listbox

            Total += myFood.hits[0].fields.nf_calories;
            TotalCalories.Text = ((int)Total).ToString(); //Adds the calories of each added food to the Total Variable and Display it
        } 

I expect the AutoSuggestBox to show me the Data from the string as being typed. For Example "Bana" - List of food pop up with the name Bana.

enter image description here

But the actual result is the AutoSuggestBox showing the ArrayData and the API Data in string being added into the ListBox as being typed.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
Znibba
  • 15
  • 4
  • *For Example "Bana" - List of food pop up with the name Bana*, I'm not really understand this mean. could you tell more detail about this. – Nico Zhu Apr 29 '19 at 03:30
  • So its a food library, which has a list of food. And full word "Banana" If you start with B and slowly type all the way to Banana, A list needs to pop up. But all in the auto suggest box. Right now it does this in a list. Heres the results [Result Image](http://prntscr.com/nij3ls) . I want the list to be in where the first arrow is, not the 2nd arrow. – Znibba Apr 30 '19 at 03:45
  • Which arrow you want to realize left arrow or down arrow? – Nico Zhu Apr 30 '19 at 06:49
  • I want the contents from the bottom arrow to show in the left arrow. So Bana Krisp Fruit Crackers Calories: 150 Protein to be showed in textbox where (Banana) is first arrow to the left. – Znibba May 01 '19 at 13:39

1 Answers1

0

I want the contents from the bottom arrow to show in the left arrow. So Bana Krisp Fruit Crackers Calories: 150 Protein to be showed in textbox where (Banana) is first arrow to the left.

For your requirement, you could get typed data with NutritionixAPI then converter the data to format string. Create a new list for AutoSuggestBox Itemsource that stored the format string.

private async void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
    switch (args.Reason)
    {
        case AutoSuggestionBoxTextChangeReason.ProgrammaticChange:
        case AutoSuggestionBoxTextChangeReason.SuggestionChosen:
            sender.ItemsSource = null;
            return;
    }
    var query = sender.Text;
    var hits = await NutritionixAPI.GetFoods(query);
    List<string> items = new List<string>();
    foreach (var hit in hits)
    {
        string temp = hit.fields.item_name + " Calories: " + hit.fields.nf_serving_size_qty + " Protein: " + hit.fields.nf_serving_size_unit + " Fat: " + hit.fields.item_id;
        if (items.Exists(p => p == temp) == false)
        {
            items.Add(temp);
        }

    }
    var Suggestion = items.Where(p => p.StartsWith(sender.Text, StringComparison.OrdinalIgnoreCase)).ToArray();
    sender.ItemsSource = Suggestion;
}
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • Thank you so much for helping! I am however coming up with a little issue. https://i.imgur.com/pfqctl7.png and it is leading to my RootObject method in my API Class https://i.imgur.com/dL8NMgu.png If you need more info on my API Class, I can screenshot the whole thing if that makes things easier. Thanks anyways for helping so much even upto this point! Legend! – Znibba May 04 '19 at 05:00
  • I implemented `NutritionixAPI` with Nutritionix v1.0. And there many different fields, You could replace it with your own model class. – Nico Zhu May 04 '19 at 05:29
  • Yes but I dont think the error is in with fields, it is to do something with the Enums (GetEnumerator) - Even when nothing is in the foreach statement, hits still shows error : https://i.imgur.com/x3tkxzH.png -------------- https://i.imgur.com/16glry4.png : This is my NutritionixAPI class, am I missing something? Sorry for this. – Znibba May 04 '19 at 09:00
  • 1
    I have got it working, just needed to name the hits field in my method so hits.hits and it all worked. Thank you so much for helping with this! – Znibba May 06 '19 at 08:28