要求赢得8.1搜索框建议

时间:2014-04-27 16:59:43

标签: c# winrt-xaml search-box

我有一个包含searchBox的userControl。 这个UserControl在另一个内部。

我在搜索时遇到了一个奇怪的行为,因为这个建议集合以一种奇怪的方式运作。

示例:

在searchBox中我写的东西都很完美,如果我选择它也可以。 但如果我尝试使用退格(在选择之后),我没有任何建议。 我无法理解它为什么不起作用。

代码

//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 

问题在于:所有变量都具有正确的值,但只有当我进行特定搜索时,才会显示建议面板:当我更改搜索的第一个字母或错误的搜索后,它会出现

进行搜索时附加的内容

that's the normal search

如果我使用退格键,那么会附加什么,以及我想要修复的内容

what i want to correct

正如我所说的那样,在"退格"之后,一切都完美无缺。行动suggestionCollection得到正确的价值......但面板丢失了。 有人能帮助我吗?

1 个答案:

答案 0 :(得分:3)

您可以使用SearchBox和SuggestionRequested事件在SearchBox上键入时触发事件。我将展示一个例子

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

并在后面的代码中编写SearchBoxEventsSuggestionsRequested处理程序

    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);
                }
            }
        }
     }

您可以将关键字添加到 SuggestioList ,当您在搜索框上输入时,它会显示在下拉列表中。

创建SuggestionList

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

初始化列表

SuggestionList = new List<string>();

并在列表中添加关键字

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

感谢。