Skip to main content

Register gesture handlers

⚠️ This is the most common source of bugs when testing gestures.

When using GestureRecognizerTestHelper or MultiTouchTestHelperFactory to test gesture events like Pinch, Scroll, or Pull-to-Refresh, handlers must be registered BEFORE the window is shown.

Register before showing the window​

var control = new MyControl();
var window = new Window { Content = control };
window.Show(); // ❌ Show FIRST

// Too late! Gesture recognizers are already initialized
Gestures.AddPinchHandler(control, (s, e) => { /* never called */ });
var control = new MyControl();

// ✅ Register handlers BEFORE Show()
Gestures.AddPinchHandler(control, (s, e) => {
Console.WriteLine($"Pinch: Scale={e.Scale}");
});

var window = new Window { Content = control };
window.Show(); // ✅ Show AFTER

// Now gestures will work
var (first, second) = MultiTouchTestHelperFactory.CreatePair();
first.Down(control, new Point(100, 150));
second.Down(control, new Point(200, 150));
// ... gesture events will fire correctly

Why Does This Happen?​

Avalonia's gesture recognizers are attached to controls when they enter the visual tree (during Show()). If handlers aren't registered at that point, the gesture recognizer doesn't know to track those events.

Which APIs Are Affected?​

APIAffected?Notes
GestureRecognizerTestHelper✅ YesUses actual gesture recognizers
MultiTouchTestHelperFactory✅ YesWraps GestureRecognizerTestHelper
TouchInputSimulator.PinchGesture()❌ NoRaises events directly via RaiseEvent
GestureSimulator.Pinch()❌ NoRaises events directly via RaiseEvent
AvaloniaDriver touch actions✅ YesUses gesture recognizer path

Debugging Tip​

If your gesture handlers aren't being called:

  1. Check that handlers are registered before window.Show()
  2. Verify the control is actually in the visual tree
  3. Use GestureSimulator if you just need to raise events without actual recognizers