locked
TappedRoutedEventArgs.GetPosition method RRS feed

  • Question

  • I need the Y coordinate of a PictureBox tapped event. In good old desktop VB PictureBox_Click event, I used simply e.Y. But it does not do with Store apps.

    I found the PictureBox_Tapped event which seems the same but no luck.I am totally lost with the GetPosition method and have no idea how to apply it.

    Would a nice soul kindly help the newbie and post a snippet that gives the Y coordinate ?

    I will be forever grateful.

    Wednesday, December 18, 2013 12:32 AM

Answers

  • GetPosition returns a position relative to a specific element. If you want the position relative to the page then pass the page in. You can then check the X and Y coordinates of the Point returned:

        Private Sub Image_Tapped(sender As Object, e As TappedRoutedEventArgs)
            Dim p As Point = e.GetPosition(Me)
            Debug.WriteLine("Tapped {0},{1}", p.X, p.Y)
    
        End Sub

    If you want the position relative to something else you can pass in the element you care about. You might want the position within your Image control:

        Private Sub Image_Tapped(sender As Object, e As TappedRoutedEventArgs)
            Dim img As Image = CType(sender, Image)
            Dim p As Point = e.GetPosition(img)
            Debug.WriteLine("Tapped {0},{1}", p.X, p.Y)
    
        End Sub

    • Marked as answer by Mitchb00 Wednesday, December 18, 2013 5:38 AM
    Wednesday, December 18, 2013 1:43 AM
    Moderator

All replies

  • GetPosition returns a position relative to a specific element. If you want the position relative to the page then pass the page in. You can then check the X and Y coordinates of the Point returned:

        Private Sub Image_Tapped(sender As Object, e As TappedRoutedEventArgs)
            Dim p As Point = e.GetPosition(Me)
            Debug.WriteLine("Tapped {0},{1}", p.X, p.Y)
    
        End Sub

    If you want the position relative to something else you can pass in the element you care about. You might want the position within your Image control:

        Private Sub Image_Tapped(sender As Object, e As TappedRoutedEventArgs)
            Dim img As Image = CType(sender, Image)
            Dim p As Point = e.GetPosition(img)
            Debug.WriteLine("Tapped {0},{1}", p.X, p.Y)
    
        End Sub

    • Marked as answer by Mitchb00 Wednesday, December 18, 2013 5:38 AM
    Wednesday, December 18, 2013 1:43 AM
    Moderator
  • Rob, you really are a great help here. I had read several time the documentation without understanding the notion of point. Your example is splendid. Now I see better how to apply the concept. Thank you so much !

    Wednesday, December 18, 2013 5:38 AM