Touch in relation to the screen. Windows phone

Hi, I am developing an application for Windows Phone 8 using the mvvm template.

What I want to find is the position relative to the screen. So I always know where the user clicked on the screen, regardless of scale or anything else. Just a position relative to the screen, because then I can calculate the size of the screen relative to the increase and position.

I want this question to be Android .

This means that I cannot use TransformToVisual, as this requires a UIElement. Anyone have an idea on this issue?

optional To emphasize the question, I know how to get the click position inside the canvas. My problem is the position, which can be many places on the screen.

For example, the position (x, y) may be in the upper left and upper right. But how can I find out where the point is relative to the screen, i.e. In which corner?

+4
source share
2 answers

I think you can try using Xna 's TouchPanel - it works very well, and I think you 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;
}

(0,0), , , .

EDIT -

, TouchInput CompositionTarget.Rendering Timer - , .
, XNA , , :

FrameworkDispatcher.Update();

EDIT 2 - Pinch

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

+4

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

+2

All Articles