如何在windows 10通用中使用c#获取用户触摸相对于屏幕的X和Y坐标
本文关键字:屏幕 相对于 触摸 坐标 用户 获取 windows 10通 | 更新日期: 2023-09-27 18:19:22
我在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,这里https://github.com/TheOliver/Windows-10-Feature-Demos/blob/master/Windows10FeatureDemos/Windows10FeatureDemos/Samples/HowToGetCoordinatesOfTouchPoints.xaml
我希望这对你有帮助。奥利弗