WPF ClickGestureRecognizer

FractorFractor Member ✭✭

Is ClickGestureRecognizer implemented for WPF yet?

If so, could someone please show a usage example?

If not, can someone please suggest a workaround?

I've tried using ClickGestureRecognizer but can't seem to get the event to fire, though tap does.

<BoxView BackgroundColor="#55555555">
    <BoxView.GestureRecognizers>
        <ClickGestureRecognizer Clicked="ClickGestureRecognizer_OnClicked" />
    </BoxView.GestureRecognizers>
</BoxView>

I need to capture a mouse click event and know the mouse position relative to the clicked element.

Tagged:

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    You can try TapGestureRecognizer. If you have used MVVM, the code could be like this:

    <BoxView BackgroundColor="#55555555">
        <BoxView.GestureRecognizers>
            <TapGestureRecognizer Command="{Binding TapCommand}"/>
        </BoxView.GestureRecognizers>
    </BoxView>
    
    // Code behind
    InitializeComponent();
    // Set the binding context
    BindingContext = new PageViewModel();
    
    public class PageViewModel
    {
        public PageViewModel()
        {
            TapCommand = new Command(() =>
            {
    
            });
        }
        public ICommand TapCommand { set; get; }
    }
    

    Or you want to consume this gesture directly on your page:

    <BoxView BackgroundColor="#55555555" >
        <BoxView.GestureRecognizers>
            <TapGestureRecognizer x:Name="MyTapGesture"/>
        </BoxView.GestureRecognizers>
    </BoxView>
    
    // Code behind
    MyTapGesture.Command = new Command(() =>
    {
    
    });
    
  • FractorFractor Member ✭✭

    Thanks. Yes tap does fire but I couldn't see how to get the mouse position.

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    @Fractor Mouse position is platform related, you have to implement it on each platform.
    https://forums.xamarin.com/discussion/17767/touch-coordinates-in-tapgesturerecognizer this case discusses how to get the positions through tap gesture. You could take a look at it.

Sign In or Register to comment.