Attaching touch event to CanvasView on android

Hi, I am new to xamarin and android development, and this is my first stab at it, so I am not sure I have got it entirely right.

My issue is that I can't get the touch event to fire.

I have gone through the documentation and guides to add touch to my canvasview, in other to pan and scale my image.
docs.microsoft.com/...../skiasharp/transforms/touch
(can't post the link)

My problem with this is that my xml is not the same, is this because I am using android XML rather than xamarin xml? Either way, I could not get it to recognise the grid I surrounded the CanvasView with and was attaching the event too. It kept telling me there was an error expanding the class.

here is my current XML:

`<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="....."

xmlns:tt="clr-namespace:TouchTracking"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<include

    android:id="@+id/toolbar"

    layout="@layout/toolbar" />

<TextView

    android:text="Gregorian Date:"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:id="@+id/textView1"

    android:textSize="12sp"

    android:padding="3dp" />

<TextView

    android:text="Metonic Date:"

    android:textSize="12sp"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:id="@+id/textView2"

    android:padding="3dp" />

<skiasharp.views.android.SKCanvasView

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:id="@+id/canvasView"

    EnableTouchEvents="True"

    Touch="OnTouchEffectAction"/>

`

(had to remove the xmlns link to post)

As you can see I have now tried to add the touch events directly to the CanvasView, and handle them in the code behind, as I read that was possible. I had to edit the code in the tutorial to use SKTouchAction and remove a xamarin.Forms.Point, but other than that is was pretty painless. Here is the method signature I found online to attach to the event. I notice that this was in a ContentView, and is this again the "xamarin forms way"?

void OnTouchEffectAction(object sender, SKTouchEventArgs args) { ...

And this doesn't seem to fire? So I tried to attach the event in the code behind

canvasView.Touch += OnTouchEffectAction;

but that seems to want a method with the signature:

void OnTouchEffectAction(object sender, View.TouchEventArgs args) { ...

So I guess that is why it is not firing from the XML.

How can I attach this touch event given my XML? I don't want to have to change to the "xamarin forms way" at this stage, I am only targeting android, and the app is almost done! What am I missing?

Posts

  • saishaiksaishaik INMember ✭✭

    Hi @RadicalGeek , I am also looking exactly as yours. Did you make it to be worked any way? If so can you please guide me with?

Sign In or Register to comment.