简体   繁体   中英

how to Get X and Y coordinates of user touch relative to screen with C# in windows 10 universal

I used this code in windows phone 8:

public partial class MainPage : PhoneApplicationPage  
{  
    // Constructor  
    public MainPage()  
    {  
        InitializeComponent();  
        Touch.FrameReported += Touch_FrameReported;  
    }  
    void Touch_FrameReported(object sender, TouchFrameEventArgs e)  
    {  
        TouchPoint touchPoint = e.GetTouchPoints(this.ContentPanel).FirstOrDefault();  
        if (touchPoint.Action == TouchAction.Up)  
        MessageBox.Show(touchPoint.Position.X + "," + touchPoint.Position.Y);//Displaying x&y co-ordinates of Touch point  
    }  
}

but is not working in windows 10 universal app.

I think you can get the result by using this code:

    private void _theCanvas_Tapped(object sender, TappedRoutedEventArgs e)
    {
        _pointerDeviceType.Text = e.PointerDeviceType.ToString();
        var position = e.GetPosition(_root);
        _x.Text = position.X.ToString();
        _y.Text = position.Y.ToString();
    }

The e.GetPosition()-Method helps you to get the position based on another control. The full sample code including the XAML is here: https://github.com/TheOliver/Windows-10-Feature-Demos/blob/master/Windows10FeatureDemos/Windows10FeatureDemos/Samples/HowToGetCoordinatesOfTouchPoints.xaml.cs & and here https://github.com/TheOliver/Windows-10-Feature-Demos/blob/master/Windows10FeatureDemos/Windows10FeatureDemos/Samples/HowToGetCoordinatesOfTouchPoints.xaml

I hope this helps. Oliver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM