Search code examples
c#xamlwin-universal-appinkcanvas

Event Handlers not working with inkCanvas in Windows Universal App


I am not able to make PointerPressed, PointerReleased (any pointer event) work.

I have written this line of code to support touch

inkCanvas.InkPresenter.InputDeviceTypes = CoreInputDeviceTypes.Mouse | CoreInputDeviceTypes.Touch;

from the solutions that I found online, I have tried this

inkCanvas.PointerReleased += inkCanvas_PointerReleased;

and this

inkCanvas.AddHandler(PointerReleasedEvent, new PointerEventHandler(inkCanvas_PointerReleased), true);

and simply through XAML as well:

     <InkCanvas x:Name="inkCanvas" 
PointerPressed="inkCanvas_PointerPressed" 
PointerMoved="inkCanvas_PointerMoved" PointerReleased="inkCanvas_PointerReleased" 
Tapped="inkCanvas_Tapped">

I am testing the app in my device (not on emulator). what is it that I am missing?


Solution

  • There are few things to consider. First When you declare InkCanvas you need to Let the UIElement know what input type it can expect. There are 3 commonly used types.

    CoreInputDeviceTypes.Mouse ( Desktop / Notebooks without touch capability )
    CoreInputDeviceTypes.Pen ( Surfacebooks and other pen enabled devices )
    CoreInputDeviceTypes.Touch ( all other devices that accept Touch )
    

    So you need to make sure these are declared on your InkCanvas.

    Once done, see if your pointerevents are fired when called from XAML. If it still does not work, Try below.

    CoreInkIndependentInputSource  core = CoreInkIndependentInputSource.Create(inkCanvas.InkPresenter);
    core.PointerPressing += Core_PointerPressing;
    core.PointerReleasing += Core_PointerReleasing;
    core.PointerMoving += Core_PointerMoving;
    

    Edit 2: Below are the methods for all 3 actions

            private void Core_PointerMoving(CoreInkIndependentInputSource sender, PointerEventArgs args)
            {
                throw new NotImplementedException();
            }
    
            private void Core_PointerReleasing(CoreInkIndependentInputSource sender, PointerEventArgs args)
            {
                throw new NotImplementedException();
            }
    
            private void Core_PointerPressing(CoreInkIndependentInputSource sender, PointerEventArgs args)
            {
                throw new NotImplementedException();
            }
    

    Hope this helps.