Пример #1
0
        protected Task ResultsForSearchString(string searchString)
        {
            return(Wrap.MethodAsync("ResultsForSearchString", async delegate()
            {
                this.InvokeOnMainThread(delegate()
                {
                    this.ResultsArray.Clear();
                    this.ResultsTable.ReloadData();
                });

                if (string.IsNullOrWhiteSpace(searchString))
                {
                    searchString = string.Empty;
                }
                searchString = searchString.Trim().ToLower();

                if (SearchMethodAsync != null)
                {
                    List <object> results = await SearchMethodAsync(searchString);
                    this.InvokeOnMainThread(delegate()
                    {
                        lock (_searchResultRoot)
                        {
                            // ensure same search is still pending
                            string currentText = this.TokenField.Text;
                            if (!string.IsNullOrEmpty(currentText))
                            {
                                currentText = currentText.Replace(TITokenField.kTextEmpty, "").Replace(TITokenField.kTextHidden, "");
                            }
                            ;
                            if (currentText == searchString)
                            {
                                this.ResultsArray.Clear();
                                foreach (object item in results)
                                {
                                    this.ResultsArray.Add(item);
                                }

                                if (this.ResultsArray.Count > 0)
                                {
                                    this.ResultsTable.ReloadData();
                                }
                            }
                        }
                    });
                }
                else
                {
                    this.InvokeOnMainThread(delegate()
                    {
                        Wrap.Method("ResultsForSearchString", delegate()
                        {
                            if (!string.IsNullOrEmpty(searchString) || ForcePickSearchResult)
                            {
                                foreach (var sourceObject in this.SourceArray)
                                {
                                    string title = this._tokenDelegateShim.SearchResultStringForRepresentedObject(this.TokenField, sourceObject);
                                    string subTitle = this._tokenDelegateShim.SearchResultSubtitleForRepresentedObject(this.TokenField, sourceObject);
                                    if (!SearchSubtitles || string.IsNullOrEmpty(subTitle))
                                    {
                                        subTitle = string.Empty;
                                    }

                                    if ((this.ForcePickSearchResult && string.IsNullOrEmpty(searchString)) ||
                                        title.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) > -1 ||
                                        subTitle.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) > -1)
                                    {
                                        bool shouldAdd = !this.ResultsArray.Contains(sourceObject);

                                        if (shouldAdd && !ShowAlreadyTokenized)
                                        {
                                            foreach (var token in this.TokenField.Tokens)
                                            {
                                                if (token.RepresentedObject == sourceObject)
                                                {
                                                    shouldAdd = false;
                                                    break;
                                                }
                                            }
                                        }
                                        if (shouldAdd)
                                        {
                                            this.ResultsArray.Add(sourceObject);
                                        }
                                    }
                                }
                            }
                            if (this.ResultsArray.Count > 0)
                            {
                                this.ResultsArray.Sort(delegate(object l, object r)
                                {
                                    string left = this._tokenDelegateShim.SearchResultStringForRepresentedObject(this.TokenField, l);
                                    string right = this._tokenDelegateShim.SearchResultStringForRepresentedObject(this.TokenField, r);
                                    return left.CompareTo(right);
                                });
                            }

                            if (this.ResultsArray.Count > 0)
                            {
                                this.ResultsTable.ReloadData();
                            }
                        });
                    });
                }
            }));
        }