JTIM
JTIM

Reputation: 2771

touch position relative to the screen. windows phone

Hey I am developing an app for Windows Phone 8 using mvvm pattern.

What I want to find is the position relative to the screen. Such that I always know where on the screen the user has pressed, no matter zoom or anything. Just position relative to screen, because then I can calculate the size of the screen relative to the zoom and position.

What I want is the same as this Android Question.

Which means I cannot use TransformToVisual as this is in need of an UIElement. Have anybody an idea for this issue?

extra To emphasise the question I know how to get a position of the click inside a canvas. My problem is a position can be many places on screen.

Such as position (x,y) can be in the top left and top right corner. But how can I know where the point is relative to the screen i.e. In which corner?

Upvotes: 4

Views: 1637

Answers (2)

Romasz
Romasz

Reputation: 29792

I think you can try to use TouchPanel from Xna - it works quite well, and I think will do what you want:

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

The top left corner is (0,0) and you get a position relative to it, no matter what orientation your app has.

EDIT - few remarks

Note that you can check for TouchInput also in CompositionTarget.Rendering or in Timer - it depends on what you want to achieve.
Note also that when you use XNA you will probably need sometimes to do:

FrameworkDispatcher.Update();

EDIT 2 - Pinch example

If you want to use Pinch or other gesture it can look like this:

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

As for the FrameworkDispatcher.Update() - I think you can call it in OnNavigetedTo().

Upvotes: 4

Satti
Satti

Reputation: 559

You need wptoolkit to get Points

Import WPToolkit From Nuget

Cmd: Install-Package WPtoolkit

Add this Code inside XAML Grid

 <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener
                Tap="GestureListener_Tap"/>
    </toolkit:GestureService.GestureListener> <TextBlock x:Name="focusBracket"
                   Text="*"
                   FontSize="48" 
                   Visibility="Collapsed" />

in .cs File

        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

Upvotes: 2

Related Questions