locked
[W8.1][C#]Touch doesn't work RRS feed

  • Question

  • Hello Everybody ! 
    I'm developping a Windows store apps 8.1 and I'm trying to insert a signature's functionnality.
    Here is my Xaml Code : 

                <Border Name="MyBorderCanvas"  Grid.Column="1" Height="70" Width="900" Background="White" CornerRadius="10" Tapped="VisaGrid_Tapped">
                    <Canvas Name="MyCanvas"   Margin="10" Background="White" HorizontalAlignment="Center" Height="60"  VerticalAlignment="Center" Width="900"/>
                </Border>

    Here is my .Cs code : 

    InkManager _inkKhaled = new Windows.UI.Input.Inking.InkManager();
            private uint _penID;
            private uint _touchID;
            private Point _previousContactPt;
            private Point currentContactPt;
            private double x1;
            private double y1;
            private double x2;
            private double y2;
           
    
    
            public RisksListView()
            {
                this.InitializeComponent();
    
                MyCanvas.PointerPressed += new PointerEventHandler(MyCanvas_PointerPressed);
                MyCanvas.PointerMoved += new PointerEventHandler(MyCanvas_PointerMoved);
                MyCanvas.PointerReleased += new PointerEventHandler(MyCanvas_PointerReleased);
                MyCanvas.PointerExited += new PointerEventHandler(MyCanvas_PointerReleased);
               
    
            }
    
            #region PointerEvents
            private void MyCanvas_PointerReleased(object sender, PointerRoutedEventArgs e)
            {
                if (e.Pointer.PointerId == _penID)
                {
                    Windows.UI.Input.PointerPoint pt = e.GetCurrentPoint(MyCanvas);
    
                    // Pass the pointer information to the InkManager. 
                    _inkKhaled.ProcessPointerUp(pt);
                }
    
                else if (e.Pointer.PointerId == _touchID)
                {
                    // Process touch input
                }
    
                _touchID = 0;
                _penID = 0;
    
                // Call an application-defined function to render the ink strokes.
    
    
                e.Handled = true;
            }
    
            private void MyCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
            {
                if (e.Pointer.PointerId == _penID)
                {
                    PointerPoint pt = e.GetCurrentPoint(MyCanvas);
    
                    // Render a red line on the canvas as the pointer moves. 
                    // Distance() is an application-defined function that tests
                    // whether the pointer has moved far enough to justify 
                    // drawing a new line.
                    currentContactPt = pt.Position;
                    x1 = _previousContactPt.X;
                    y1 = _previousContactPt.Y;
                    x2 = currentContactPt.X;
                    y2 = currentContactPt.Y;
    
                    if (Distance(x1, y1, x2, y2) > 2.0) // We need to developp this method now 
                    {
                        Line line = new Line()
                        {
                            X1 = x1,
                            Y1 = y1,
                            X2 = x2,
                            Y2 = y2,
                            StrokeThickness = 4.0,
                            Stroke = new SolidColorBrush(Colors.Black)
                        };
    
                        _previousContactPt = currentContactPt;
    
                        // Draw the line on the canvas by adding the Line object as
                        // a child of the Canvas object.
                        MyCanvas.Children.Add(line);
    
                        // Pass the pointer information to the InkManager.
                        _inkKhaled.ProcessPointerUpdate(pt);
                    }
                }
    
                else if (e.Pointer.PointerId == _touchID)
                {
                    // Process touch input
                }
    
            }
    
            private double Distance(double x1, double y1, double x2, double y2)
            {
                double d = 0;
                d = Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
                return d;
            }
    
            private void MyCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
            {
                // Get information about the pointer location.
                PointerPoint pt = e.GetCurrentPoint(MyCanvas);
                _previousContactPt = pt.Position;
    
                // Accept input only from a pen or mouse with the left button pressed. 
                PointerDeviceType pointerDevType = e.Pointer.PointerDeviceType;
                if (pointerDevType == PointerDeviceType.Pen ||
                        pointerDevType == PointerDeviceType.Mouse &&
                        pt.Properties.IsLeftButtonPressed)
                {
                    // Pass the pointer information to the InkManager.
                    _inkKhaled.ProcessPointerDown(pt);
                    _penID = pt.PointerId;
    
                    e.Handled = true;
                }
    
                else if (pointerDevType == PointerDeviceType.Touch)
                {
                    // Process touch input
                }
            }
    
            #endregion

    The problem is that when I'm using the mouse it works but when I'm using the finger it doesn't work.

    Someone may help me please in telling where the mistake is and how to handle it?

    Thank you !

    Monday, September 28, 2015 3:23 PM

Answers

  • Your code explicitly limits input to only pen or mouse. If you don't want that, don't do that: // Accept input only from a pen or mouse with the left button pressed. PointerDeviceType pointerDevType = e.Pointer.PointerDeviceType; if (pointerDevType == PointerDeviceType.Pen || pointerDevType == PointerDeviceType.Mouse && pt.Properties.IsLeftButtonPressed) {
    • Marked as answer by DiddyRennes Monday, September 28, 2015 3:48 PM
    Monday, September 28, 2015 3:31 PM

All replies

  • Your code explicitly limits input to only pen or mouse. If you don't want that, don't do that: // Accept input only from a pen or mouse with the left button pressed. PointerDeviceType pointerDevType = e.Pointer.PointerDeviceType; if (pointerDevType == PointerDeviceType.Pen || pointerDevType == PointerDeviceType.Mouse && pt.Properties.IsLeftButtonPressed) {
    • Marked as answer by DiddyRennes Monday, September 28, 2015 3:48 PM
    Monday, September 28, 2015 3:31 PM
  • Oups !!...You right! :)
    Monday, September 28, 2015 3:48 PM