c# - 在 Xamarin Forms 中使用 TextChanged 事件后,SearchBar 会自动取消焦点
问题描述
我有一个简单的搜索栏组件及其TextChanged事件
<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()));
}
解决方案
当您在 Collection\ListView 的 Header 中使用 SearchBar 时,似乎会出现此问题,因此只需移到它之外。
推荐阅读
- javascript - 使用 Ajax 选择不加载选项
- r - 每次我想在 R 中使用 foreach() 时,是否必须注册 DoParallel() 和 stopCluster()?
- geolocation - 室内记录对象位置
- asp.net-core - 在 .netcore 中的控制器外部获取请求
- sql-server - 从另一列更新除年以外的日期和月份
- gitlab - 无法推送到 gitlab,失败并被拒绝
- java - 使 BorderLayout 中的项目占用正确的大小
- xml - 在 Spring Batch 中将资源传递给处理器
- r - 当 API 调用在 Shiny 中返回空时显示错误消息?
- jquery - 在表内显示 ajax jquery 响应