【问题标题】:SearchBar unfocus automatically after using TextChanged event in Xamarin Forms在 Xamarin Forms 中使用 TextChanged 事件后,SearchBar 会自动取消焦点
【发布时间】:2020-08-15 05:42:30
【问题描述】:

我有一个简单的 search-bar 组件及其 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()));
        }

【问题讨论】:

    标签: c# xamarin.forms xamarin.android searchbar


    【解决方案1】:

    您可以尝试让您的搜索栏手动聚焦。

     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();
        }
    

    【讨论】:

    • 在没有数据采集和过滤的情况下无法重现该问题,当我处理 SearchBar_TextChanged mryhod 时搜索栏仍然焦点。
    【解决方案2】:

    当您在 Collection\ListView 的 Header 中使用 SearchBar 时似乎会出现此问题,因此请移到它之外。

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 2021-09-11
      • 2018-12-01
      • 2018-10-13
      相关资源
      最近更新 更多