1

I'm sending CommandParameter to TapGestureRecognizer, and it was available in object parameter of my OnTapGestureRecognizerTapped function. But I'm unable to extract value from it. How can I fix this?

My list view (XAML markup):

<ListView x:Name="listViewEvents" RowHeight="120" HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid Padding="5">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="40"></RowDefinition>
                        <RowDefinition Height="60"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="90"></ColumnDefinition>
                    </Grid.ColumnDefinitions>

                    <Label Grid.Row="0" Grid.Column="0" FontSize="Large" Grid.ColumnSpan="2"  Text="{Binding UploadTitle}" LineBreakMode="TailTruncation"></Label>
                    <Label Grid.Row="1" Grid.Column="0" Text="{Binding UploadDescription}" Font="Small"  TextColor="Gray" LineBreakMode="TailTruncation"></Label>
                    <Image Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" Source="{Binding AttachPath}" Aspect="AspectFill">
                        <Image.GestureRecognizers>
                            <TapGestureRecognizer
                                Tapped="OnTapGestureRecognizerTapped"
                                NumberOfTapsRequired="2"
                                CommandParameter="{Binding AttachPath_New}"
                                />
                        </Image.GestureRecognizers>
                    </Image>
                    <Label Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding FileName}" LineBreakMode="TailTruncation"></Label>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

My C# page :

void OnTapGestureRecognizerTapped(object sender, EventArgs args)
{
        string fullPath = "";
        Image imageSender = (Image)sender;

        if (imageSender.GestureRecognizers.Count > 0)
        {
            fullPath = imageSender.GestureRecognizers[0].ToString();
        }
        DownloadFile(imageSender.Source.GetValue(UriImageSource.UriProperty).ToString());
}

Here I'm getting my value:

Enter image description here

2 Answers 2

6

Another solution you can use is to typecast EventArgs to TappedEventArgs and expose the Parameter. So to access the CommandParameter you would write the following:

private void OnTapped(object sender, EventArgs e)
{
var te = (TappedEventArgs)e;
string parameter = (string)te.Parameter;
....
4

You are trying to convert the entire GestureRecognizer to a string, not just its CommandParameter property.

void OnTapGestureRecognizerTapped(object sender, EventArgs args)
{
    string fullPath = "";
    Image imageSender = (Image)sender;
    if (imageSender.GestureRecognizers.Count > 0)
    {
        var gesture = (TapGestureRecognizer)imageSender.GestureRecognizers[0];
        fullPath = (string)gesture.CommandParameter;
    }
    DownloadFile(imageSender.Source.GetValue(UriImageSource.UriProperty).ToString());
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.