【问题标题】:Win 8.1 SearchBox SuggestionsRequested已请求 Win 8.1 搜索框建议
【发布时间】:2014-05-06 21:52:05
【问题描述】:

我有一个包含搜索框的用户控件。 这个 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 得到了正确的值......但是面板丢失了。 有人可以帮我吗?

【问题讨论】:

  • 你得到答案了吗?
  • 对不起,我没有解决方案,这仍然是一个悬而未决的问题,你找到有用的东西了吗?

标签: c# winrt-xaml search-box


【解决方案1】:

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

创建建议列表

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

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2011-02-21
    相关资源
    最近更新 更多