Xamarin.forms: ICommand binding from a TapGestureRecognizer on a Span doesn't work

Created on 9 Aug 2018  路  6Comments  路  Source: xamarin/Xamarin.Forms

Description

I tried out the new gesture recognizer functionality on Spans that's part of 3.2. When binding to an ICommand from the Command property of a TapGestureRecognizer, the ICommand isn't executed when a tap occurs. This is true for both XAML and C# implementations.

                    <Span Text="default, " Style="{DynamicResource BodyStyle}">
                        <Span.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binding TapCommand}" />
                        </Span.GestureRecognizers>
                    </Span>
            var span = new Span { Text = "default, " };
            var tapGesture = new TapGestureRecognizer();
            tapGesture.SetBinding(TapGestureRecognizer.CommandProperty, "TapCommand");
            span.GestureRecognizers.Add(tapGesture);
            formattedString.Spans.Add(span);

Steps to Reproduce

  1. Run the attached sample. It will launch the LabelPage.
  2. Tap the "default" text, in the final Label (this Label comprises three Spans).

Expected Behavior

The TapGestureRecognizer detects the tap and calls the execute delegate of the bound ICommand, which displays an alert.

Actual Behavior

Nothing happens.

Basic Information

  • Version with issue: 3.2.0.637442-pre1
  • IDE: VSMac
  • Platform Target Frameworks:

    • iOS: 11.4

    • Android: 8.1

    • UWP: Not tried.

  • Android Support Library Version: 27.0.2

Reproduction Link

Text.zip

blocker 3 bug

All 6 comments

Linking another issue I found
https://github.com/xamarin/Xamarin.Forms/issues/2892
In case the fix turns out to be related

This is also happening to all my Xamarin Forms Apps (v3.4.x, Android, UWP, and iOS). The attached TapGestureRecognizer in the spans do not work.

I've encountered this issua a while ago and now tried again (forgot it was a thing) and happen to "discover" a way to make it work and probably a hit why it happens in the first place

this code won't work and the command won't be triggered

            <Label>
                <Label.FormattedText>
                    <FormattedString>
                        <Span Text="{Binding Word.Noun, StringFormat='{0} (n.)'}">
                            <Span.GestureRecognizers>
                                <TapGestureRecognizer Command="{Binding PronounceWordCommand}" CommandParameter="{Binding .}" />
                            </Span.GestureRecognizers>
                        </Span>
                        <Span Text="&#x0a;" />
                        <Span Text="{Binding Word.Adjective, StringFormat='{0} (adj.)'}">
                            <Span.GestureRecognizers>
                                <TapGestureRecognizer Command="{Binding PronounceWordCommand}" CommandParameter="{Binding .}" />
                            </Span.GestureRecognizers>
                        </Span>
                    </FormattedString>
                </Label.FormattedText>
            </Label>

but, changing the binding from "." to the actual text makes it work!

<TapGestureRecognizer Command="{Binding PronounceWordCommand}" CommandParameter="{Binding Word.Adjective}" />

Has anyone tested a Span with a TapGestureRecognizer on iOS in Landscape orientation? It doesn't seem to be working as expected.

when text direction is RTL command takes more than one tap to execute?

Has anyone tested a Span with a TapGestureRecognizer on iOS in Landscape orientation? It doesn't seem to be working as expected.

I'm observing this issue on a iPad. TapGestureRecognizer works when the device is in Portrait mode, but not when it is in Landscape mode. Odd indeed.

Observed with Xamarin Forms version 4.2.709249.

Seems related to this #4143

Was this page helpful?
0 / 5 - 0 ratings