Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to get screen X and Y values from a 'Touch' event on a SkiaSharp CanvasView. The event fires okay, but the eventargs e does not let me access the X and Y values directly. Instead, I have to dig them out of the string e.toString() as follows -
void OnTouch(object sender, EventArgs e)
     {
         string str = e.ToString();
         int index = str.IndexOf("X=");
         string strX = str.Substring(index + 2);
         index = strX.IndexOf(",");
         strX = strX.Remove(index);
         index = str.IndexOf("Y=");
         string strY = str.Substring(index + 2);
         index = strY.IndexOf("}");
         strY = strY.Remove(index);
         float X = Convert.ToSingle(strX);
         float Y = Convert.ToSingle(strY);
         offsetX += centX - X;
         if (offsetX > 2000)
             offsetX = 2000;
         else if (offsetX < -2000)
             offsetX = -2000;
         offsetY += centY - Y;

         if (!timer.Enabled)
         {
             canvasView.InvalidateSurface();
         }
     }

Here is the XAML -
XML
<sk:SKCanvasView x:Name="canvasView" Grid.Row="1" Grid.ColumnSpan="4" PaintSurface="OnCanvasViewPaintSurface" BackgroundColor="Black" EnableTouchEvents="True" Touch="OnTouch">
</sk:SKCanvasView>

This works, but it's kind of ugly. There must be a better way.

What I have tried:

Searched the web for answer, but no luck.
Posted
Updated 8-Nov-21 17:03pm

1 solution

You're using the "wrong" signature (and namespace?) to get the event argument for ".Location" (x and y). You want SKTouchEventArgs.

SKCanvasView.OnTouch(SKTouchEventArgs) Method (SkiaSharp.Views.Forms) | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Member 11298827 9-Nov-21 11:27am    
Gerry, thanks so much for your reply. I changed the type of 'e' to SKTouchEventArgs, and was then able to easily access e.Location.X and e.Location.Y
[no name] 9-Nov-21 12:17pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900