嘿我正在使用mvvm模式为Windows Phone 8开发应用程序。
我想要找的是相对于屏幕的位置。这样我总是知道用户按下屏幕的位置,无论是缩放还是其他任何东西。只需相对于屏幕定位,因为那时我可以计算屏幕相对于缩放和位置的大小。
我想要的与此Android Question相同。
这意味着我不能使用TransformToVisual,因为这需要一个UIElement。有人对这个问题有所了解吗?
额外 为了强调这个问题,我知道如何在画布中获得点击的位置。我的问题是一个位置可以在屏幕上的很多地方。
例如位置(x,y)可以在左上角和右上角。但是我怎么知道这个点相对于屏幕的位置,即在哪个角落?
答案 0 :(得分:4)
我认为你可以尝试使用Xna的TouchPanel - 它运作得很好,我认为会做你想做的事情:
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework;
public MainPage()
{
InitializeComponent();
TouchPanel.EnabledGestures = GestureType.Tap;
this.Tap += MainPage_Tap;
}
private void MainPage_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
GestureSample gesture = TouchPanel.ReadGesture();
Vector2 vector = gesture.Position;
int positionX = (int)vector.X;
int positionY = (int)vector.Y;
}
左上角是(0,0),无论您的应用在哪个方向,您都会获得相对于它的位置。
编辑 - 几句评论
请注意,您也可以在CompositionTarget.Rendering或Timer中检查TouchInput - 这取决于您想要实现的目标。
另请注意,使用XNA时,有时可能需要执行以下操作:
FrameworkDispatcher.Update();
编辑2 - 拼接示例
如果您想使用Pinch或其他手势,它可能如下所示:
public MainPage()
{
InitializeComponent();
TouchPanel.EnabledGestures = GestureType.Pinch;
this.ManipulationCompleted+=MainPage_ManipulationCompleted;
}
private void MainPage_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
{
if (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
Vector2 vector = gesture.Position;
int positionX = (int)vector.X;
int positionY = (int)vector.Y;
// do what you want with your positin
// there are also some more properties in which you may be interested
// also TouchPanel has properites like TouchPanel.DisplayWidth and DisplayHeight if you need them
}
}
至于FrameworkDispatcher.Update() - 我想你可以在OnNavigetedTo()中调用它。
答案 1 :(得分:2)
你需要wptoolkit才能获得积分
从Nuget导入WPToolkit
Cmd:Install-Package WPtoolkit
在XAML Grid中添加此代码
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener
Tap="GestureListener_Tap"/>
</toolkit:GestureService.GestureListener> <TextBlock x:Name="focusBracket"
Text="*"
FontSize="48"
Visibility="Collapsed" />
<。>在.cs文件中
private void GestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
{
try
{
Point tapLocation = e.GetPosition(viewfinderCanvas);
if (tapLocation != null)
{
focusBracket.SetValue(Canvas.LeftProperty,tapLocation.X);
focusBracket.SetValue(Canvas.TopProperty, tapLocation.Y);
double tapX = tapLocation.X;
double tapY = tapLocation.Y;
focusBracket.Visibility = Visibility.Visible;
this.Dispatcher.BeginInvoke(delegate()
{
this.txtDebug.Text = string.Format("Tapping Coordinates are X={0:N2}, Y={1:N2}", tapX, tapY);
});
}
}
catch (Exception error){
this.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = error.Message;
});
}
}