ListView内的SearchBar没有响应

时间:2017-12-13 12:20:38

标签: xamarin xamarin.forms

我的ListView顶部有SearchBar来过滤项目。要求是SearchBar滚动就像项目一样,所以我将SearchBar添加到我的ListView

当我点击SearchBar时,没有任何反应。即使我向TapGestureRecognizer添加SearchBar,它也不会执行抽头方法。

ListView

<ListView 
CachingStrategy="RecycleElement"
BackgroundColor="{StaticResource listBackgroundColor}"
Grid.Row="1"
Grid.Column="0"
x:Name="listViewUsers"
ItemsSource="{Binding Users}"
ItemTemplate="{StaticResource KontaktDataTemplateSelector}"
ItemTapped="Handle_ItemTapped"
IsPullToRefreshEnabled="false"
SeparatorColor="{StaticResource listSeparatorColor}">
    <ListView.RowHeight>
        <OnPlatform x:TypeArguments="x:Int32">
            <On Platform="iOS">88</On>
            <On Platform="Android">72</On>
        </OnPlatform>
    </ListView.RowHeight>
    <ListView.Margin>
        <OnPlatform x:TypeArguments="Thickness">
            <OnPlatform.iOS>
                0,10,0,0
            </OnPlatform.iOS>
            <OnPlatform.Android>
                0,0,0,0
            </OnPlatform.Android>
            <OnPlatform.WinPhone>
                -10,0,0,0
            </OnPlatform.WinPhone>
        </OnPlatform>
    </ListView.Margin>
</ListView>

ViewCell非常基本:

<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
          xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms" 
          prism:ViewModelLocator.AutowireViewModel="True" 
          xmlns:i18n="clr-namespace:One;assembly=One"
          x:Class="One.Views.SuchenViewCell">
    <SearchBar 
        Placeholder="{i18n:Translate Text=suchen}"
        Margin="5, 0, 0, 0"
        Grid.Row="0"
        Grid.Column="0"
        Text="{Binding Suchtext}"
        Style="{StaticResource searchBarStyle}"
        TextChanged="Handle_TextChanged">
        <SearchBar.GestureRecognizers> // only for test, but even this doesn't get executed
            <TapGestureRecognizer
                NumberOfTapsRequired="1"
                Tapped="SearchBar_Tapped">
            </TapGestureRecognizer>
        </SearchBar.GestureRecognizers>
    </SearchBar>
</ViewCell>

我在SearchBar

中添加DataTemplateSelector这样的内容
public class KontaktDataTemplateSelector : DataTemplateSelector
{
    private readonly DataTemplate kontaktViewCellDataTemplate;
    private readonly DataTemplate suchenViewCellDataTemplate;

    public KontaktDataTemplateSelector()
    {
        kontaktViewCellDataTemplate = new DataTemplate(typeof(KontaktViewCell));
        suchenViewCellDataTemplate = new DataTemplate(typeof(SuchenViewCell));
    }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        if (item == null)
        {
            return null;
        }

        UserDto userDto = item as UserDto;

        if (userDto.IsSuchenViewCell)
        {
            return suchenViewCellDataTemplate;
        }
        else
        {
            return kontaktViewCellDataTemplate;
        }
    }
}

SearchBar添加到ListView时,ListView无效的任何想法?如果我将它放在ListView之外,它会起作用,但它不会滚动并停留在{{1}}之上,这不符合要求。

3 个答案:

答案 0 :(得分:1)

您需要在ListView标题中指定搜索栏的HeightRequest和宽度请求

 <ListView.Header>
    <SearchBar HeightRequest="40" WidthRequest="150">...</SearchBar>
 </ListView.Header>

答案 1 :(得分:0)

如何将searchBar放入ListView?

我认为如果你将searchBar放在ListView的Header中就行了。

答案 2 :(得分:0)

尝试在listview的标题中添加SearchBar,如下所示:

<ListView.Header>
   <SearchBar>...</SearchBar>
</ListView.Header>