in ContosoApp/Views/CustomerListPage.xaml.cs [77:110]
private async void CustomerSearchBox_TextChanged(AutoSuggestBox sender,
AutoSuggestBoxTextChangedEventArgs args)
{
// We only want to get results when it was a user typing,
// otherwise we assume the value got filled in by TextMemberPath
// or the handler for SuggestionChosen.
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
// If no search query is entered, refresh the complete list.
if (String.IsNullOrEmpty(sender.Text))
{
await dispatcherQueue.EnqueueAsync(async () =>
await ViewModel.GetCustomerListAsync());
sender.ItemsSource = null;
}
else
{
string[] parameters = sender.Text.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
sender.ItemsSource = ViewModel.Customers
.Where(customer => parameters.Any(parameter =>
customer.Address.StartsWith(parameter, StringComparison.OrdinalIgnoreCase) ||
customer.FirstName.StartsWith(parameter, StringComparison.OrdinalIgnoreCase) ||
customer.LastName.StartsWith(parameter, StringComparison.OrdinalIgnoreCase) ||
customer.Company.StartsWith(parameter, StringComparison.OrdinalIgnoreCase)))
.OrderByDescending(customer => parameters.Count(parameter =>
customer.Address.StartsWith(parameter) ||
customer.FirstName.StartsWith(parameter) ||
customer.LastName.StartsWith(parameter) ||
customer.Company.StartsWith(parameter)))
.Select(customer => $"{customer.FirstName} {customer.LastName}");
}
}
}