1

我有一个包含搜索框的用户控件。这个 UserControl 在另一个里面。

我在搜索时遇到了一个奇怪的行为,因为SuggestionCollection 以一种奇怪的方式工作。

例子 :

在搜索框中,我写的东西都可以正常工作,如果我选择它也可以工作。但是,如果我尝试使用退格键(在选择之后),我没有得到任何建议。我不明白为什么它不起作用。

那是代码

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

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

当我进行搜索时会附加什么

这是正常的搜索

如果我使用退格键会附加什么,我想修复什么

我要纠正的

正如我所说,一切正常,在“退格”操作后suggestionCollection得到了正确的值......但面板丢失了。有人可以帮助我吗?

4

1 回答 1

3

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

<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,当您在搜索框中键入时,它会显示在下拉列表中。

创建建议列表

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

谢谢。

于 2014-05-06T13:52:05.433 回答