locked
How can I use GestureRecognizer in a Xaml based app?

    Question

  • I've encountered a problem when I'm trying to use gestures in my app:

    I'm trying to retrieve info from input on a rectangle using GestureRecognizer responding to the PointerPressed event. When the user taps on it, the textblock shows information. However, I failed to get any info after following the code samples. There was no response and through debugging, I found the tapped event was not responded to.

    First, I added:

    Platform::Agile<Windows::UI::Input::GestureRecognizer> m_gestureRecognizer;

    in the header of the class.

    And in the constructor:

    MainPage::MainPage()
    {
    	InitializeComponent();
    	m_gestureRecognizer = ref new GestureRecognizer();
    
    	m_gestureRecognizer->GestureSettings =
    		GestureSettings::DoubleTap | GestureSettings::Tap;
    
    	m_gestureRecognizer->Tapped += ref new Windows::Foundation::TypedEventHandler<Windows::UI::Input::GestureRecognizer ^, Windows::UI::Input::TappedEventArgs ^>(this, &App4::MainPage::OnTapped);	
    }

    Next, I responded to the PointerPressed event:

    void App4::MainPage::OnPressed(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
    {
    	unsigned int id = e->Pointer->PointerId;//debug breakpoint
    	m_gestureRecognizer->ProcessDownEvent(Windows::UI::Input::PointerPoint::GetCurrentPoint(id));
    	e->Handled = true;
    }

    But when I came to the Tapped event, nothing happened:

    void MainPage::OnTapped(GestureRecognizer^ gesrec, Windows::UI::Input::TappedEventArgs^ args)
    {
    	int i = args->TapCount;//debug breakpoint
    	if (i == 2)
    	{
    		info->Text = "You double-tapped the point at X: " + args->Position.X + ", Y: " + args->Position.Y + ".";
    	}
    	else
    	{
    		info->Text = "You tapped the point X: " + args->Position.X + ", Y: " + args->Position.Y + ".";
    	}
    }

    Why there was no info when I tapped the rectangle? Do I lack of something in the code? 

    Saturday, April 11, 2015 4:49 AM

Answers

  • Oh, it's very kind of you! Thank you so much and the problem has been solved by adding two handlers:

    void App4::MainPage::OnMoved(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
    { 
    	m_gestureRecognizer->ProcessMoveEvents(e->GetIntermediatePoints(wpRect/*name of the rectangle*/));
    }
    
    
    void App4::MainPage::OnReleased(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
    {
    	m_gestureRecognizer->ProcessUpEvent(Windows::UI::Input::PointerPoint::GetCurrentPoint(e->Pointer->PointerId));
    }

    I've been haunted by this issue for days...

    • Marked as answer by Sal Leerhoy Saturday, April 11, 2015 7:03 AM
    Saturday, April 11, 2015 7:02 AM

All replies

  • You need to pass the PointerMoved and PointerReleased events to the GestureRecognizer so it has full information to recognize the gestures.

    Tap is a down and up, so if you only pass through down then it won't be generated.

    Saturday, April 11, 2015 5:51 AM
    Moderator
  • Oh, it's very kind of you! Thank you so much and the problem has been solved by adding two handlers:

    void App4::MainPage::OnMoved(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
    { 
    	m_gestureRecognizer->ProcessMoveEvents(e->GetIntermediatePoints(wpRect/*name of the rectangle*/));
    }
    
    
    void App4::MainPage::OnReleased(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
    {
    	m_gestureRecognizer->ProcessUpEvent(Windows::UI::Input::PointerPoint::GetCurrentPoint(e->Pointer->PointerId));
    }

    I've been haunted by this issue for days...

    • Marked as answer by Sal Leerhoy Saturday, April 11, 2015 7:03 AM
    Saturday, April 11, 2015 7:02 AM