PanGestureRecognizer - no event fired

MLyrstadMLyrstad Member ✭✭

Hello folks!

I'm having trouble getting the PanGestureRecognizer to work. The recognizer is added to a FlexLayout in a custom control. This is the control's control template:

 <ControlTemplate x:Key="controltemplates/jumplist">
    <Grid>
        <ContentPresenter />
        <FlexLayout x:Name="PART_ItemHost"
                    BackgroundColor="Red"
                    Direction="Column"
                    HorizontalOptions="End"
                    VerticalOptions="Fill"
                    Margin="0, 0, 0, 0"
                    WidthRequest="32"
                    BindableLayout.ItemsSource="{TemplateBinding Groups}">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding Text}"
                           TextColor="#AAAAAA"
                           HorizontalTextAlignment="Center"
                           FontFamily="{StaticResource fonts/bold}"
                           BackgroundColor="Transparent"
                           InputTransparent="True"
                           FlexLayout.Grow="1"
                           FontSize="13"
                           HorizontalOptions="Start"
                           WidthRequest="24"
                           Margin="0">
                    </Label>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </FlexLayout>
    </Grid>
</ControlTemplate>

The recognizer is added to the flexlayout during the control's OnApplyTemplate method. I have verified that the method is called, the layout is found, and the recognizer is added. However I do not get any PanUpdated events.

Does anyone have an idea?

Thank you in advance.

Best Answer

  • MLyrstadMLyrstad ✭✭
    Accepted Answer

    Because it is a proper custom control, not a "user control" or a view with viewmodel and code behind. The control is defining the behavior. It feels unnatural to me to add the gestures in Xaml because of that, I can't bind the gestures to anything in any way in xaml . So the host component isn't available to me until OnApplyTemplate.

    I think my solution must be to create a per-platform component which deals with the gestures more closely to the metal. What I need simply isn't available in a cross-platform way.

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited August 14

    I'm not sure that if the pan gesture will work when added on Flexlayout , but you could try to add gesture on the item that bound to FlexLayout , refer https://taubensee.net/adding-touch-events-to-flexlayouts/.

  • MLyrstadMLyrstad Member ✭✭

    Thank you for answering.

    Why would the gesture not work on the layout? I also do not understand what you mean by "the item that bound to FlexLayout". Also, that document is from 2014 and as such is very dated.

    I tried to wrap a ContentView around the flexlayout akin to
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/pan

    but even that didn't work.

    Interesting. When I added the gesture in xaml, and added to its event in the code-behind afterwards, it worked. There's indications that this is quite simply too late which is a problem.

  • ColeXColeX Member, Xamarin Team Xamurai
    edited August 14

    You mean it works only when added in xaml , but doesn't work when put the code by overriding OnApplyTemplate Method ?
    Why you add the gesture in OnApplyTemplate ?

  • MLyrstadMLyrstad Member ✭✭
    Accepted Answer

    Because it is a proper custom control, not a "user control" or a view with viewmodel and code behind. The control is defining the behavior. It feels unnatural to me to add the gestures in Xaml because of that, I can't bind the gestures to anything in any way in xaml . So the host component isn't available to me until OnApplyTemplate.

    I think my solution must be to create a per-platform component which deals with the gestures more closely to the metal. What I need simply isn't available in a cross-platform way.

  • ColeXColeX Member, Xamarin Team Xamurai

    ok , try to put the login in method OnMeasure to see if the gesture works.

  • MLyrstadMLyrstad Member ✭✭

    Thank you very much for trying to help, ColeX, it is very appreciated. I hit another roadblock though, with the level of control I have with the gestures (I need to detect "pressed" as well), I'll have to resort to per-platform.

Sign In or Register to comment.