如何在windows 10 universal中使用C#获取用户触摸相对于屏幕的X和Y坐标

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

我在 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  
    }  
}

但无法在 windows 10 通用应用程序中使用。

我想你可以使用这段代码得到结果:

    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();
    }

e.GetPosition()-方法可帮助您根据另一个控件获取位置。包括 XAML 的完整示例代码在这里: 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

希望对您有所帮助。 奥利弗