[英]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()-Method可帮助您基于另一个控件获取位置。 包括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
我希望这有帮助。 奥立佛
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.