Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What event do i use to trigger something like a mousedown in XAML (XamarinForms)

I want to be able to fire an event when the user is doing like a mousedown on a grid. (Like a visual feedback that the user is tapping the grid but not a click!)

I have this XAML code:

 <Grid.Triggers>
    <EventTrigger Event="MouseDown">
        <local:MouseOver/>
    </EventTrigger>
</Grid.Triggers>

And this in a c# class:

 public class MouseOver : TriggerAction<Entry>
{
    protected async override void Invoke(Entry entry)
    {
        //Some animation code here
    }
}

Well i am never able to get in the event, even though the class is implemented correctly! I also tried "IsMouseOver" and "MouseEnter" as Events but none of that seems to work. I hope someone here can help me... Thx for your help!

like image 826
Cambaru Avatar asked Dec 04 '25 14:12

Cambaru


1 Answers

EventTrigger only works for events that are present on the Grid class (or one of it's super classes). In XForms there aren't any mouse events as it's mostly designed for touch interfaces.

To add tap handling to a control i XForms is done by adding a TapGestureRecognizer to the GestureRecognizers collection on the control.

// In C#
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
    // handle the tap
};
image.GestureRecognizers.Add(tapGestureRecognizer);

// In XAML
<Grid Source="tapped.jpg">
    <Grid.GestureRecognizers>
        <TapGestureRecognizer
                Tapped="OnTapGestureRecognizerTapped"
                NumberOfTapsRequired="2" />
  </Grid.GestureRecognizers>
</Grid>

You can read up on in the XForms docs.

like image 142
Jon G Stødle Avatar answered Dec 07 '25 02:12

Jon G Stødle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!