2014-01-07 33 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