如何使用参数添加tapGestureRecognizer?

时间:2017-09-16 08:06:12

标签: xamarin xamarin.forms

我的代码目前在后端C#

中添加了这个
deselectGridLink.GestureRecognizers.Add(NewTapGestureForUpdateCategories(false));

    private TapGestureRecognizer NewTapGestureForUpdateCategories(bool val)
    {
        return new TapGestureRecognizer()
        {
            Command = new Command(() =>
            {
                App.DB.UpdateAllCategoryGroups(val);
                App.DB.UpdateAllCategories(val);
                GetPageData();
                RemoveTableViewClickSection();
                tableView.Root.Add(CreateTableSection());
                SetPageDetails();
            })
        };
    }

如何在XAML中添加此参数,同时包含false参数。此外,如果我以某种方式将此添加到XAML,我是否需要更改C#f NewTapGestureForUpdateCategories方法?:

<Grid x:Name="deselectGridLink" VerticalOptions="CenterAndExpand" Padding="20, 0">
   <Label TextColor="Blue" Style="{DynamicResource ListItemTextStyle}" x:Name="deselectLink" HorizontalOptions="StartAndExpand" VerticalOptions="Center" Text="Deselect All" />
</Grid>

2 个答案:

答案 0 :(得分:3)

XAML:

<Grid x:Name="deselectGridLink" VerticalOptions="CenterAndExpand" Padding="20, 0">
    <Label TextColor="Blue" 
           Style="{DynamicResource ListItemTextStyle}" 
           x:Name="deselectLink" 
           HorizontalOptions="StartAndExpand" 
           VerticalOptions="Center" 
           Text="Deselect All" >
     <Label.GestureRecognizers>
        //!!!!!!!!!!!!!!!!!!!!!!!!!set parameter here.
        <TapGestureRecognizer Command="{Binding TapCommand}" CommandParameter="false"/>
     </Label.GestureRecognizers>

    </Label>
</Grid>

代码背后:

public partial class YourPage : ContentPage
{
    public Command TapCommand
    {
        get
        {
            return new Command(val => {
                DisplayAlert("Alert", val.ToString(), "OK");
            });
        }
    }

    public YourPage()
    {
        InitializeComponent();
        this.BindingContext = this;
    }
}

在Label.GestureRecognizers中设置 CommandParameter 的值时,后面代码中的TapCommand可以接收它。

答案 1 :(得分:2)

<ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <Grid Padding="10">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="30"></RowDefinition>
                                            <RowDefinition Height="*"></RowDefinition>
                                            <RowDefinition Height="*"></RowDefinition>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                            <ColumnDefinition Width="50"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>

                                        <Label Grid.Row="0" Grid.Column="0" VerticalTextAlignment="Center" Text="{Binding UserName}" LineBreakMode="TailTruncation" TextColor="{StaticResource DarkTextColor}" FontSize="Medium"></Label>
                                        <Label Grid.Row="1" Grid.Column="0" VerticalTextAlignment="Center" Text="{Binding Mobile}" TextColor="{StaticResource DarkTextColor}" FontSize="Small" />
                                        <Label Grid.Row="2" Grid.Column="0" VerticalTextAlignment="Center" Text="{Binding Email}" TextColor="{StaticResource DarkTextColor}" FontSize="Small" />
                                        <Label Grid.Column="1" Grid.RowSpan="3" IsVisible="{Binding IsAdmin}" HorizontalOptions="Center" VerticalOptions="Center"
                                               TextColor="Red" FontSize="Large"
                                               Text="&#xf2ed;" FontFamily="Font Awesome 5 Free-Solid-900.otf#Font Awesome 5 Free Regular" >
                                            <Label.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="Status_Clicked" CommandParameter="{Binding UserId}"/>
                                            </Label.GestureRecognizers>
                                        </Label>
                                    </Grid>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>




async private void Status_Clicked(object sender, EventArgs e)
        {
                    Label lblClicked = (Label)sender;
                    var item = (TapGestureRecognizer)lblClicked.GestureRecognizers[0];
                    var id=item.CommandParameter;
        }