How to use both GestureRecognizers and Effects in Xamarin Forms at the same time?

I am creating a custom control. I want to change the Background Color When Touched using Custom Effects in the Native Code and also handle the TapGesture Event in the Event Handler in the backend code. But When I do this only one of these two is happening (ie. When I define Grid.GestureRecognizer first and then Grid.Effect then Grid.Effect is only getting executed and vice versa). As both are dependent on OnTouch Events one is getting overridden by the other. How to tackle this.

In Xaml file:
<.Grid >
<Grid.GestureRecognizers>
<TapGestureRecognizerTapped="OnTapGestureRecognizerTapped"/>
</Grid.GestureRecognizers>
<Grid.Effects>

</Grid.Effects>
//OtherCode
</.Grid>

In Backend Code:
event EventHandler Click;

             void OnTapGestureRecognizerTapped(object sender, EventArgs args)
                    {
                        if (Click != null)
                            Click.Invoke(this, args);
                    }

In Native Code The Effect.cs

            protected override void OnAttached()
            {
                Container.SetOnTouchListener(new MyOnTouchListener());
            }

              public class MyOnTouchListener : Java.Lang.Object, Android.Views.View.IOnTouchListener
                    {
                        public bool OnTouch(Android.Views.View v, MotionEvent e)
                        {
                            switch (e.Action)
                            {
                    }
                       }
                 }

Or is there anyother efficient way of doing this?

Answers

  • AjaySawantAjaySawant INMember ✭✭

    Hi Jack,

    I am stumbled across the same issue, You got any solution for this ?

    Thanks in Advance
    Ajay Sawant

  • ReihanehKhaksaranReihanehKhaksaran USMember ✭✭
    edited April 2018

    I also have the same issue, I have added a tap gesture recognizer to my StackLayout and I want to change the background color when it is tapped so that the user recognizes that the layout has been tapped

  • KrunalKrunal USMember ✭✭
    edited May 2018

    still no fix for this problem?? I'm also witnessing same problem. Effects and gesturerecognizers don't work together

  • dengyejingdengyejing USMember ✭✭

    same problem!
    I want to add TapGestureRecognizer and LongPressEffect to the same BoxView, but only one works.

  • CreamyMiracleCreamyMiracle Member ✭✭

    Same problem here!

  • CreamyMiracleCreamyMiracle Member ✭✭

    Well, I just added another effect for plain click and everything seems to work now! Ask if you need more details about the implementation :)

  • lescperlescper Member ✭✭

    @CreamyMiracle said:
    Well, I just added another effect for plain click and everything seems to work now! Ask if you need more details about the implementation :)

    Hi @CreamyMiracle .I think it can not work. and I add another stacklayout above it .can you share your implementation?

  • CreamyMiracleCreamyMiracle Member ✭✭
    edited February 6

    @lescper
    Yes, in XAML I have just defined two effects for my UI element, in this case for Frame:

                         <Frame.Effects>
                              <effects:LongPressedEffect />
                              <effects:PressedEffect />
                         </Frame.Effects>
    

    And those effects are implemented just like in the post that you can find by googling:
    "Adding long press gesture recognizer in Xamarin forms"
    ( I can't add links yet to my posts)

    Only difference to the example I referred, is that I also created a second almost identical class, where I just set:
    Control.LongClickable = true; => Control.Clickable = true;
    Control.LongClick += Control_LongClick; => Control.Click += Control_Click;
    AND
    Container.LongClickable = true; => Container.Clickable = true;
    Container.LongClick += Control_LongClick; => Container.Click += Control_Click;

  • lescperlescper Member ✭✭

    @CreamyMiracle said:
    @lescper
    Yes, in XAML I have just defined two effects for my UI element, in this case for Frame:



    <Frame.Effects>


    </Frame.Effects>

    And those effects are implemented just like in the post that you can find by googling:
    "Adding long press gesture recognizer in Xamarin forms"
    ( I can't add links yet to my posts)

    Only difference to the example I referred, is that I also created a second almost identical class, where I just set:
    Control.LongClickable = true; => Control.Clickable = true;
    Control.LongClick += Control_LongClick; => Control.Click += Control_Click;
    AND
    Container.LongClickable = true; => Container.Clickable = true;
    Container.LongClick += Control_LongClick; => Container.Click += Control_Click;

    good job.

Sign In or Register to comment.