2014-01-07 35 views
4

嘿,我正在开发一个使用mvvm模式的Windows Phone 8应用程序。触摸相对于屏幕的位置。 windows phone

我想找到的是相对于屏幕的位置。这样,我总是知道用户按下了屏幕上的哪个位置,不管是变焦还是其他东西。只是相对于屏幕的位置,因为那样我就可以计算屏幕相对于缩放和位置的大小。我想要的就是这个Android Question

这意味着我不能使用TransformToVisual,因为这是需要一个UIElement。有没有人有这个问题的想法?

extra 为了强调这个问题,我知道如何在画布中获得点击的位置。我的问题是一个位置可以在屏幕上的很多地方。

比如位置(x,y)可以在左上角和右上角。但是,我怎么知道点在哪个角落?

回答

4

我想你可以尝试使用TouchPanel从XNA - 它工作得很好,我想会做你想要什么:

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或定时检查TOUCHINPUT - 这取决于你想要达到的目标。
还要注意,当你使用XNA你可能有时候需要做的:

FrameworkDispatcher.Update(); 

EDIT 2 - 捏例如

如果你想使用捏或其它手势也可以是这样的:

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()。

+0

看起来很有趣,我会试着看看明天。并回报。 – JTIM

+0

我试过你的代码,看起来像它做我想要的。但这不是我需要的事件。我希望它为捏事件。然而,我一直无法做到这一点,你能帮我把它转换为使用捏来代替吗?还FrameworkDispatcher.Update,应该何时调用? – JTIM

+0

@JTIM我在编辑2中添加了,你问了什么。另请参阅其他手势类型,也许您会对它们感兴趣。我也添加捏到ManipulationCompleted - 请注意,它可以是ManipalationStarted,或delta,如果你需要它们。 – Romasz

2

你需要wptoolkit拿分

导入WPToolkit从的NuGet

CMD:Install-Package WPtoolkit

添加以下代码XAML网格内

<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; 

     }); 

     } 

    } 

Result

+0

谢谢你我会尝试并实施它,并返回:) – JTIM

+0

这是为我工作听到.. –

+0

这似乎是点相对于画布? viewfinderCanvas? 由于我可以在画布内获得我的位置,但我需要知道这个位置如何与屏幕相关。这是代码的作用?我会测试它,但似乎它是相对于viewfinderCanvas? – JTIM