l33t
l33t

Reputation: 19937

Detect tap in page?

I have a PhoneApplicationPage with some grids, images and a few buttons. If I tap outside my buttons, anywhere in the page (on an image, grid or whatever), I want to open a new page.

How can I detect a tap/click anywhere on the page?

Upvotes: 0

Views: 1055

Answers (1)

Kevin Gosse
Kevin Gosse

Reputation: 39007

I see at least two ways to do that:

  1. Listen to the MouseLeftButtonUp event on your PhoneApplicationPage. It should be triggered by images and labels but not by buttons
  2. Listen to the Tap event on your PhoneApplicationPage. However, this event will be triggered even when the user tap on a button. To prevent this, you can retrieve the list of controls at the tap coordinates in the event handler, and open the new page only if there's no button:

    private void PhoneApplicationPage_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        var element = (UIElement)sender;
    
        var controls = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(element), element);
    
        if(controls.OfType<Button>().Any())
        {
            return;
        }
    
        // Open the new page
    }
    

Upvotes: 1

Related Questions