[英]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.