ArghArgh
ArghArgh

Reputation: 373

Win 8.1 SearchBox SuggestionsRequested

I got an userControl that contains a searchBox. This UserControl is inside another one.

I got a strange behavior while i'm searching, because the suggestionCollection works in a strange way.

Example :

in the searchBox i write something all works perfectly, if i choose the item it also works. But if i try to use backspace (after the choose) i got no suggestion. I cannot understand why it doesn't work.

That's the code

//var deferral = args.Request.GetDeferral();  //it seems to not influence the behavior 
            var suggestionCollection = args.Request.SearchSuggestionCollection;
            
            try
            {

                TransporterExt tr_search = new TransporterExt();

                //queryText is a string inserted in the searchBox

                if (string.IsNullOrEmpty(queryText)) return;

                tr_search.name = queryText;

                suggested.Clear();  //that's a collection..

                //just a search that return a collection of objects TransporterExt
                querySuggestions = await TransporterService.Search_StartsWith(tr_search);
                
                if (querySuggestions.Count > 0)
                {

                    int i = 0;
                    foreach (TransporterExt tr in querySuggestions)
                    {
                        string name = tr.name;
                        string detail = tr.trId.ToString();
                        string tag = i.ToString();
                        string imageAlternate = "imgDesc";
                        suggestionCollection.AppendResultSuggestion(name, detail, tag, imgRef, imageAlternate);
                        this.suggested.Add(tr);

                        i++;
                    }

                }
                
            }
            catch (System.ArgumentException exc)
            {
                //Ignore any exceptions that occur trying to find search suggestions.
                Debug.WriteLine(exc.Message);
                Debug.WriteLine(exc.StackTrace);
            }
 //deferralComplete();  //it seems to not influence the behavior 

The problem is that: all the variables have the right value, but the suggestion panel appears only if i make a particular search: it appears when i change the first letter of search, or after an wrong seach

What appends when i make a search

that's the normal search

What appends if i use the backspace, and i what i want to fix

what i want to correct

As i said, all works perfectly, after the "backspace" action the suggestionCollection got the right value...but the panel is missing. Could someone help me?

Upvotes: 1

Views: 592

Answers (1)

Rashad Valliyengal
Rashad Valliyengal

Reputation: 3162

You can use SearchBox and SuggestionRequested event to fire the event when type on the SearchBox. I will show an Example

<SearchBox x:Name="SearchBoxSuggestions" SuggestionsRequested="SearchBoxEventsSuggestionsRequested"/>

and write the SearchBoxEventsSuggestionsRequested handler in the code behind

    private void SearchBoxEventsSuggestionsRequested(object sender, SearchBoxSuggestionsRequestedEventArgs e)
    {
        string queryText = e.QueryText;
        if (!string.IsNullOrEmpty(queryText))
        {
            Windows.ApplicationModel.Search.SearchSuggestionCollection suggestionCollection = e.Request.SearchSuggestionCollection;
            foreach (string suggestion in SuggestionList)
            {
                if (suggestion.StartsWith(queryText, StringComparison.CurrentCultureIgnoreCase))
                {
                    suggestionCollection.AppendQuerySuggestion(suggestion);
                }
            }
        }
     }

You can add the keyword to SuggestioList, and it will show in the dropdown when you type on the Searchbox.

Create the SuggestionList

public List<string> SuggestionList { get; set; }

initialize the list

SuggestionList = new List<string>();

and add keywords to the list

SuggestionList.Add("suggestion1");
SuggestionList.Add("suggestion2");
SuggestionList.Add("suggestion3");
SuggestionList.Add("suggestion4");
SuggestionList.Add("Fruits");

Thanks.

Upvotes: 3

Related Questions