SearchBox结果建议Windows 8.1

时间:2014-04-10 20:31:41

标签: c# xaml winrt-xaml

我的Windows 8.1 App中的searchBox出现了一个奇怪的问题。

如果在我的建议中我没有附加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:在意外时间调用了一个方法。 exception:System.InvalidOperationException - type(string)

  

编辑解决方案 - 工作职能

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

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

1 个答案:

答案 0 :(得分:2)

您可以使用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");

当您在搜索框上键入s时,它将显示所有关键字以s开头。

感谢。

相关问题