예제 #1
0
        protected override void OnAttached()
        {
            base.OnAttached();

            // Initializes the manipulation processor.
            _manipulationProcessor = new ManipulationProcessor2D(Manipulations2D.All);
            _manipulationProcessor.Started += OnManipulationStarted;
            _manipulationProcessor.Delta += OnManipulationDelta;
            _manipulationProcessor.Completed += OnManipulationCompleted;

            // Initializes the touch helper.
            TouchHelper.AddHandlers(AssociatedObject, new TouchHandlers
            {
                TouchDown = OnTouchDown,
                CapturedTouchReported = OnCapturedTouchReported
            });
            TouchHelper.EnableInput(true);
            TouchHelper.SetRootElement(TouchHelper.GetRootElement(AssociatedObject));

            _renderTransform = AssociatedObject.RenderTransform as CompositeTransform;
            if (_renderTransform == null)
            {
                throw new InvalidCastException();
            }

            _initialCenter = new Point(_renderTransform.CenterX, _renderTransform.CenterY);
        }
예제 #2
0
        private void OnCapturedTouchReported(object sender, TouchReportedEventArgs e)
        {
            var parent = AssociatedObject.Parent as UIElement;
            if (parent == null)
            {
                return;
            }

            var root = TouchHelper.GetRootElement(parent);
            if (root == null)
            {
                return;
            }

            List<Manipulator2D> manipulators = null;

            if (e.TouchPoints.FirstOrDefault() != null)
            {
                // Get transformation to convert positions to the parent's coordinate system.
                var transform = root.TransformToVisual(parent);
                foreach (var touchPoint in e.TouchPoints)
                {
                    var position = touchPoint.Position;

                    // Convert to the parent's coordinate system.
                    position = transform.Transform(position);

                    // Create a manipulator.
                    var manipulator = new Manipulator2D(
                        touchPoint.TouchDevice.Id,
                        (float)(position.X),
                        (float)(position.Y));

                    if (manipulators == null)
                    {
                        // Lazy initialization.
                        manipulators = new List<Manipulator2D>();
                    }

                    manipulators.Add(manipulator);
                }
            }

            // Process manipulations.
            _manipulationProcessor.ProcessManipulators(DateTime.UtcNow.Ticks, manipulators);
        }