示例#1
0
            /// <summary>
            /// Used during a pinch operation to scale the content for a fast pinch effect.  This does not actually change
            /// the layout of the content, so this can be done very quickly.  When the pinch completes, ResizeContent() will
            /// update the content's layout with the final zoom ratio.
            /// </summary>
            private void ScaleContentDuringPinch()
            {
                double newMaxWidth  = Math.Round(_size.Width * _zoomRatioDuringPinch);
                double newMaxHeight = Math.Round(_size.Height * _zoomRatioDuringPinch);

                double scaler = newMaxWidth / _zoomableContent.MaxWidth;

                double newWidth  = _actualContentSize.Width * scaler;
                double newHeight = _actualContentSize.Height * scaler;

                _transform.ScaleX = scaler;
                _transform.ScaleY = scaler;

                _viewport.Bounds = new Rect(0, 0, newWidth, newHeight);

                // Pan the Viewport such that the point on the content where the pinch started doesn't move on the screen
                //
                Point pinchMidpointOnScaledContent = new Point(
                    newWidth * _pinchMidpointInPercentOfContent.X,
                    newHeight * _pinchMidpointInPercentOfContent.Y);

                Point origin = new Point(
                    (int)(pinchMidpointOnScaledContent.X - _pinchMidpointInControlCoordinates.X),
                    (int)(pinchMidpointOnScaledContent.Y - _pinchMidpointInControlCoordinates.Y));

                if (origin != _lastOrigin)
                {
                    _viewport.SetViewportOrigin(origin);
                    _lastOrigin = origin;
                }
            }
示例#2
0
        private void ResizeImage(bool center)
        {
            if (_coercedScale != 0 && _bitmap != null)
            {
                var newWidth  = Canvas.Width = Math.Round(_textureScale.Width * _coercedScale);
                var newHeight = Canvas.Height = Math.Round(_textureScale.Height * _coercedScale);

                Xform.ScaleX    = Xform.ScaleY = _coercedScale;
                Viewport.Bounds = new Rect(0, 0, newWidth, newHeight);

                if (center)
                {
                    Viewport.SetViewportOrigin(new Point(
                                                   Math.Round((newWidth - Viewport.ActualWidth) / 2),
                                                   Math.Round((newHeight - Viewport.ActualHeight) / 2)
                                                   ));
                }
                else
                {
                    var newImgMid = new Point(newWidth * _relativeMidpoint.X, newHeight * _relativeMidpoint.Y);
                    var origin    = new Point(newImgMid.X - _screenMidpoint.X, newImgMid.Y - _screenMidpoint.Y);
                    Viewport.SetViewportOrigin(origin);
                }
            }
        }
        /// <summary>
        /// Adjust the size of the image according to the coerced scale factor. Optionally
        /// center the image, otherwise, try to keep the original midpoint of the pinch
        /// in the same spot on the screen regardless of the scale.
        /// </summary>
        /// <param name="center">Center the image on the viewport</param>
        private void ResizeImage(bool center)
        {
            if (_coercedScale != 0 &&
                null != _bitmap &&
                null != _imagePanel &&
                null != Image &&
                null != _viewport)
            {
                // Store, and change the width and height of the canvas to fit the size of the new image.
                double newWidth  = _imagePanel.Width = Math.Round(_bitmap.PixelWidth * _coercedScale);
                double newHeight = _imagePanel.Height = Math.Round(_bitmap.PixelHeight * _coercedScale);

                // Scale the image by _coercedScale amount
                // Values between 0 and 1 decrease the width/height of the scaled object;
                // Values greater than 1 increase the width/height of the scaled object.
                // A value of 1 indicates that the object is not scaled in the x/y-direction.
                // Negative values flip the scaled object horizontally/vertically.
                // Values between 0 and -1 flip the scale object and decrease its width/height.
                // Values less than -1 flip the object and increase its width/height.
                // A value of -1 flips the scaled object but does not change its horizontal/vertical size.
                // Scale both the width and the height simultaneously to preserve the aspect ratio.
                if (null == Image.RenderTransform || !(Image.RenderTransform is ScaleTransform))
                {
                    Image.RenderTransform = new ScaleTransform();
                }
                if (null == Image.RenderTransformOrigin)
                {
                    Image.RenderTransformOrigin = new Point(0.0d, 0.0d);
                }
                ScaleTransform xform = (ScaleTransform)Image.RenderTransform;
                xform.ScaleX = xform.ScaleY = _coercedScale;

                _viewport.Bounds = new Rect(0, 0, newWidth, newHeight);

                if (center)
                {
                    _viewport.SetViewportOrigin(
                        new Point(
                            Math.Round((newWidth - _viewport.ActualWidth) / 2),
                            Math.Round((newHeight - _viewport.ActualHeight) / 2)
                            ));
                }
                else
                {
                    Point newImgMid = new Point(newWidth * _relativeMidpoint.X, newHeight * _relativeMidpoint.Y);
                    Point origin    = new Point(newImgMid.X - _screenMidpoint.X, newImgMid.Y - _screenMidpoint.Y);
                    _viewport.SetViewportOrigin(origin);
                }
            }
        }
        public void ScrollToTop()
        {
            Rect rect = this.Viewport.Viewport;
            // ISSUE: explicit reference operation
            double y = rect.Y;

            rect = this._viewport.Bounds;
            // ISSUE: explicit reference operation
            double top1 = rect.Top;

            if (y == top1)
            {
                return;
            }
            if (this.ItemsSource != null && this.ItemsSource.Count > 0)
            {
                this.ScrollTo(this.ItemsSource[0]);
            }
            ViewportControl viewport = this.Viewport;
            double          num      = 0.0;
            Rect            bounds   = this._viewport.Bounds;
            // ISSUE: explicit reference operation
            double top2  = ((Rect)@bounds).Top;
            Point  point = new Point(num, top2);

            viewport.SetViewportOrigin(point);
            this.UpdateScrollPosition();
        }
示例#5
0
        private void DoScrollToTopOrBottom(bool toBottom, Action onCompletedCallback)
        {
            Rect   rect;
            double num1;

            if (!toBottom)
            {
                num1 = 0.0;
            }
            else
            {
                rect = this._vpCtrl.Bounds;
                // ISSUE: explicit reference operation
                num1 = rect.Bottom - ((FrameworkElement)this._vpCtrl).ActualHeight;
            }
            double           to = num1;
            ViewportMediator viewportMediator = new ViewportMediator();

            viewportMediator.ViewportControl = this._vpCtrl;
            this._vpCtrl.ViewportChanged    -= (new EventHandler <ViewportChangedEventArgs>(this._vpCtrl_ViewportChanged));
            rect = this._vpCtrl.Viewport;
            // ISSUE: explicit reference operation
            double             y   = rect.Y;
            double             to1 = to;
            DependencyProperty verticalOffsetProperty = ViewportMediator.VerticalOffsetProperty;
            int       duration  = 250;
            int?      startTime = new int?(0);
            CubicEase cubicEase = new CubicEase();
            Action    completed = (Action)(() =>
            {
                ViewportControl vpCtrl = this._vpCtrl;
                Rect viewport = this._vpCtrl.Viewport;
                // ISSUE: explicit reference operation
                Point point = new Point(((Rect)@viewport).X, to);
                vpCtrl.SetViewportOrigin(point);
                this._vpCtrl.ViewportChanged += (new EventHandler <ViewportChangedEventArgs>(this._vpCtrl_ViewportChanged));
                if (onCompletedCallback == null)
                {
                    return;
                }
                onCompletedCallback();
            });
            int num2 = 0;

            ((DependencyObject)viewportMediator).Animate(y, to1, verticalOffsetProperty, duration, startTime, cubicEase, completed, num2 != 0);
        }
示例#6
0
        void OnScrollToRequested(object sender, ScrollToRequestedEventArgs e)
        {
            if (_animatable == null && e.ShouldAnimate)
            {
                _animatable = new Animatable();
            }

            if (_viewport == null)
            {
                // Making sure we're actually loaded
                if (VisualTreeHelper.GetChildrenCount(_listBox) == 0)
                {
                    RoutedEventHandler handler = null;
                    handler = (o, args) =>
                    {
                        Control.Loaded -= handler;
                        OnScrollToRequested(sender, e);
                    };
                    Control.Loaded += handler;

                    return;
                }
                _viewport = (ViewportControl)VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(_listBox, 0), 0), 0);
                if (_viewport.Viewport.Bottom == 0)
                {
                    EventHandler <ViewportChangedEventArgs> viewportChanged = null;
                    viewportChanged = (o, args) =>
                    {
                        if (_viewport.Viewport.Bottom == 0)
                        {
                            return;
                        }

                        _viewport.ViewportChanged -= viewportChanged;
                        OnScrollToRequested(sender, e);
                    };
                    _viewport.ViewportChanged += viewportChanged;
                    return;
                }
            }

            double y                  = 0;
            double targetHeight       = 0;
            double targetHeaderHeight = 0;

            var templateReusables = new Dictionary <System.Windows.DataTemplate, FrameworkElement>();

            var found = false;

            if (Element.IsGroupingEnabled)
            {
                for (var g = 0; g < Element.TemplatedItems.Count; g++)
                {
                    if (found)
                    {
                        break;
                    }

                    TemplatedItemsList <ItemsView <Cell>, Cell> til = Element.TemplatedItems.GetGroup(g);

                    double headerHeight = GetHeight(templateReusables, Control.GroupHeaderTemplate, til);
                    y += headerHeight;

                    for (var i = 0; i < til.Count; i++)
                    {
                        Cell cell = til[i];

                        double contentHeight = GetHeight(templateReusables, Control.ItemTemplate, cell);

                        if ((ReferenceEquals(til.BindingContext, e.Group) || e.Group == null) && ReferenceEquals(cell.BindingContext, e.Item))
                        {
                            targetHeaderHeight = headerHeight;
                            targetHeight       = contentHeight;
                            found = true;
                            break;
                        }

                        y += contentHeight;
                    }
                }
            }
            else
            {
                for (var i = 0; i < Element.TemplatedItems.Count; i++)
                {
                    Cell cell = Element.TemplatedItems[i];

                    double height = GetHeight(templateReusables, Control.ItemTemplate, cell);

                    if (ReferenceEquals(cell.BindingContext, e.Item))
                    {
                        found        = true;
                        targetHeight = height;
                        break;
                    }

                    y += height;
                }
            }

            if (!found)
            {
                return;
            }

            ScrollToPosition position = e.Position;

            if (position == ScrollToPosition.MakeVisible)
            {
                if (y >= _viewport.Viewport.Top && y <= _viewport.Viewport.Bottom)
                {
                    return;
                }
                if (y > _viewport.Viewport.Bottom)
                {
                    position = ScrollToPosition.End;
                }
                else
                {
                    position = ScrollToPosition.Start;
                }
            }

            if (position == ScrollToPosition.Start && Element.IsGroupingEnabled)
            {
                y = y - targetHeaderHeight;
            }
            else if (position == ScrollToPosition.Center)
            {
                y = y - (_viewport.ActualHeight / 2 + targetHeight / 2);
            }
            else if (position == ScrollToPosition.End)
            {
                y = y - _viewport.ActualHeight + targetHeight;
            }

            double startY   = _viewport.Viewport.Y;
            double distance = y - startY;

            if (e.ShouldAnimate)
            {
                var animation = new Animation(v => { _viewport.SetViewportOrigin(new System.Windows.Point(0, startY + distance * v)); });

                animation.Commit(_animatable, "ScrollTo", length: 500, easing: Easing.CubicInOut);
            }
            else
            {
                _viewport.SetViewportOrigin(new System.Windows.Point(0, y));
            }
        }