问题描述
<SearchBar x:Name="SearchBar" Placeholder="Search words..."
CancelButtonColor="Black"
TextColor="Black"
PlaceholderColor="Black"
BackgroundColor="Transparent"
FontSize="Medium"
TextChanged="SearchBar_TextChanged"
/>
在SearchBar_TextChanged()
方法中,我尝试通过文本查找数据并将其分配给项目源。一切正常,但问题是搜索栏在过滤器之后变得没有焦点。
private async void SearchBar_TextChanged(object sender,TextChangedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.NewTextValue))
words.ItemsSource = await wordRepository.GetAllAsync();
else
words.ItemsSource = await wordRepository.Find(x => x.Name.ToLower().Contains(e.NewTextValue.ToLower()));
}
解决方法
您可以尝试让搜索栏手动聚焦。
private async void SearchBar_TextChanged(object sender,TextChangedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.NewTextValue)){
words.ItemsSource = await wordRepository.GetAllAsync();
}else{
words.ItemsSource = await wordRepository.Find(x => x.Name.ToLower().Contains(e.NewTextValue.ToLower()));
}
SearchBar.Focus();
}