点击事件不触发Android中的命令

时间:2019-05-23 06:42:05

标签: xaml xamarin xamarin.forms xamarin.android

我有xamarin形式的listview模板,如下所示

&_tbody {
    height:calc(100vh - 350px);
    overflow:auto;
    display:block;
    font-size: 12px;
    width:calc(100vh - 350px);
    overflow-x: scroll
}

并且我已经声明了以下命令:

<ListView 
x:Name="lstSections"
Grid.Row="0"
Grid.Column="1"
ItemsSource="{Binding DataList}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid Margin="0,0,0,10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <input:CheckBox IsEnabled="False" HorizontalOptions="FillAndExpand" IsChecked="{Binding Selected}" Grid.Column="0"></input:CheckBox>
                    <StackLayout VerticalOptions="CenterAndExpand" Grid.Column="1">
                        <Label Text="{Binding Name}" VerticalOptions="Fill" HorizontalOptions="StartAndExpand">
                        </Label>
                        <!--<Label Text="{Binding Data.Description}" VerticalOptions="Fill" FontSize="10" HorizontalOptions="StartAndExpand" />-->
                    </StackLayout>
                    <Grid.GestureRecognizers>
                        <ClickGestureRecognizer Command="{Binding ShowSectionCommand}" CommandParameter="{Binding Id}" />
                        <TapGestureRecognizer Command="{Binding ShowSectionCommand}" CommandParameter="{Binding Id}" />
                    </Grid.GestureRecognizers>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.Footer>
        <Button Text="Save" Command="{Binding FinishCommand}" />
    </ListView.Footer>
</ListView>

该命令在uwp应用程序上可以正常运行,但是在Android上不起作用, 请提出一些解决此问题的指示。

1 个答案:

答案 0 :(得分:1)

这是因为您没有在正确的上下文中绑定它,所以GestureRecognizor中的DataTemplate应该看起来像这样:

 <Grid.GestureRecognizers>  
 <TapGestureRecognizer Command="{Binding BindingContext.ShowSectionCommand, Source={x:Reference lstSections}}" CommandParameter="{Binding Id}" />
 </Grid.GestureRecognizers>