in XamlControlsGallery/SearchResultsPage.xaml.cs [72:143]
private void BuildFilterList(string queryText)
{
if (!string.IsNullOrEmpty(queryText))
{
// Application-specific searching logic. The search process is responsible for
// creating a list of user-selectable result categories:
var filterList = new List<Filter>();
// Query is already lowercase
var querySplit = queryText.ToLower().Split(" ");
foreach (var group in ControlInfoDataSource.Instance.Groups)
{
var matchingItems =
group.Items.Where(item =>
{
// Idea: check for every word entered (separated by space) if it is in the name,
// e.g. for query "split button" the only result should "SplitButton" since its the only query to contain "split" and "button"
// If any of the sub tokens is not in the string, we ignore the item. So the search gets more precise with more words
bool flag = true;
foreach (string queryToken in querySplit)
{
// Check if token is in title or subtitle
if (!item.Title.ToLower().Contains(queryToken) && !item.Subtitle.ToLower().Contains(queryToken))
{
// Neither title nor sub title contain one of the tokens so we discard this item!
flag = false;
}
}
return flag;
}).ToList();
int numberOfMatchingItems = matchingItems.Count();
if (numberOfMatchingItems > 0)
{
filterList.Add(new Filter(group.Title, numberOfMatchingItems, matchingItems));
}
}
if (filterList.Count == 0)
{
// Display informational text when there are no search results.
VisualStateManager.GoToState(this, "NoResultsFound", false);
var textbox = NavigationRootPage.Current.PageHeader?.GetDescendantsOfType<AutoSuggestBox>().FirstOrDefault();
textbox?.Focus(FocusState.Programmatic);
}
else
{
// When there are search results, set Filters
var allControls = filterList.SelectMany(s => s.Items).ToList();
filterList.Insert(0, new Filter("All", allControls.Count, allControls, true));
Filters = filterList;
// Check to see if the current query matches the last
if (_queryText == queryText)
{
// If so try to restore any previously selected pivot item
if (_pivotIndex != null)
{
resultsPivot.SelectedIndex = _pivotIndex.Value;
}
}
else
{
// Otherwise reset query text and pivot index
_queryText = queryText;
_pivotIndex = null;
}
VisualStateManager.GoToState(this, "ResultsFound", false);
}
}
}