SearchBox结果建议Windows 8.1

本文关键字:Windows 结果 SearchBox | 更新日期: 2023-09-27 17:57:45

我在Windows 8.1应用程序中的searchBox出现了一个奇怪的问题。

如果在Suggestion中我没有附加querySuggestion[查询建议],而只附加ResultSuggestion。

当我更改queryText时会出现问题。

这是我的功能

public async void OnSuggest(Windows.UI.Xaml.Controls.SearchBox e, SearchBoxSuggestionsRequestedEventArgs args)
    {
        var deferral = args.Request.GetDeferral();
        var queryText = args.QueryText != null ? args.QueryText.Trim() : null;
        if (string.IsNullOrEmpty(queryText)) return;
        TransporterExt tr_search = new TransporterExt();
        tr_search.name = queryText;

        try
        {

            var suggestionCollection = args.Request.SearchSuggestionCollection;
            ObservableCollection<TransporterExt> querySuggestions = await TransporterService.Search(tr_search);

            if (querySuggestions != null && querySuggestions.Count > 0)
            {
                foreach (TransporterExt tr in querySuggestions)
                {
                    //if (tr.name.ToUpperInvariant().Contains(e.QueryText.ToUpperInvariant()))
                    //{
                    //    //suggestionCollection.AppendQuerySuggestion(tr.name);
                    //    suggestionCollection.AppendResultSuggestion(tr.name,
                    //                                                 tr.trId.ToString(),
                    //                                                 tr.trId.ToString(),
                    //                                                 imgRef, "imgDesc");

                    //}
                    suggestionCollection.AppendQuerySuggestion(tr.name);

                }
            }
        }
        catch (Exception)
        {
            //Ignore any exceptions that occur trying to find search suggestions.
        }
        deferral.Complete();
    }

我在UserControl 中找到了searchBox

我的控制器代码

public delegate void SuggestionsRequested(Windows.UI.Xaml.Controls.SearchBox sender, SearchBoxSuggestionsRequestedEventArgs args);
    public event Windows.Foundation.TypedEventHandler<Windows.UI.Xaml.Controls.SearchBox, SearchBoxSuggestionsRequestedEventArgs> SearchBoxSuggestionsRequested;
    private void SearchBoxSuggestions(Windows.UI.Xaml.Controls.SearchBox sender, SearchBoxSuggestionsRequestedEventArgs args)
    {
         if (SearchBoxSuggestionsRequested != null)
            SearchBoxSuggestionsRequested(sender, args);

    }

我得到了这个异常

WinRT:在意外的时间调用了一个方法。异常:System.InvalidOperationException-类型(字符串)

编辑的解决方案-工作功能

首先,我从页面的构造函数中删除事件的注册

public TruckCrudPage()
    {
        this.InitializeComponent();
        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += navigationHelper_LoadState;
        this.navigationHelper.SaveState += navigationHelper_SaveState;
        //this.truckForm.SearchBoxSuggestionsRequested += OnSuggest;
    }
public async void OnSuggest(Windows.UI.Xaml.Controls.SearchBox e, SearchBoxSuggestionsRequestedEventArgs args)
    {
        var deferral = args.Request.GetDeferral();
        TransporterExt tr_search = new TransporterExt();
        ObservableCollection<TransporterExt> querySuggestions = new ObservableCollection<TransporterExt>();
        var queryText = args.QueryText != null ? args.QueryText.Trim() : null;
        if (string.IsNullOrEmpty(queryText)) return;
        suggested.Clear();
        tr_search.name = queryText;
        try
        {

            var suggestionCollection = args.Request.SearchSuggestionCollection;

             querySuggestions = await TransporterService.Search(tr_search);    

            if (querySuggestions != null && querySuggestions.Count > 0 )
             {
                 int i = 0;
                 foreach (TransporterExt tr in querySuggestions)
                 {
                     if (tr.name.StartsWith(e.QueryText, StringComparison.CurrentCultureIgnoreCase))
                     //if (tr.name.ToLower().Contains(e.QueryText))
                     {
                         string name = tr.name;
                         string detail = tr.trId.ToString(); 
                         string tag = i.ToString(); 
                         string imageAlternate = "imgDesc";
                         suggestionCollection.AppendResultSuggestion(name, detail, tag, imgRef, imageAlternate);
                         suggested.Add(tr);
                         //Debug.WriteLine("dentro" + suggested.Count);
                         i++;
                     }

               }
            }
        }
        catch (Exception exc)
        {
            //Ignore any exceptions that occur trying to find search suggestions.
             Debug.WriteLine("Exception generata " + exc.Message);
             Debug.WriteLine(exc.StackTrace);
        }
        deferral.Complete();
    }

但它只适用于StartsWith条件,我想使用Contains

SearchBox结果建议Windows 8.1

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

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

并在后面的代码中写入SearchBoxEventsSuggestationsRequested处理程序

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

当你在搜索框中键入s时,它会显示所有以s开头的关键字。

谢谢。