Пример #1
0
        private void HandleMoveInfoByPosition(ControlActionInfo controlActionInfo, MoveInfoByPosition moveInfo)
        {
            var    control  = controlActionInfo.Control;
            var    timeSpan = moveInfo.TimeSpan;
            double width    = Double.IsNaN(control.Width) ? 0 : control.Width;
            double height   = Double.IsNaN(control.Height) ? 0 : control.Height;

            if (timeSpan.TotalMilliseconds == 0) // 如果时间间隔为0 那么直接对位置进行赋值
            {
                this.RaiseEvent(new ActionEventArgs(MovingStartEvent, this)
                {
                    Storyboard = null, ActionInfo = moveInfo
                });
                controlActionInfo.InAction = true;
                control.SetValue(Canvas.LeftProperty, moveInfo.DestX - width / 2);
                control.SetValue(Canvas.TopProperty, moveInfo.DestY - height / 2);
                controlActionInfo.InAction = false;
                this.RaiseEvent(new ActionEventArgs(MovingEndEvent, this)
                {
                    Storyboard = null, ActionInfo = moveInfo
                });
                HandleActionInfo(controlActionInfo);
            }
            else
            {
                var storyboard = new Storyboard();

                var xda = new DoubleAnimation();
                Storyboard.SetTarget(xda, control);
                Storyboard.SetTargetProperty(xda, new PropertyPath(Canvas.LeftProperty));
                xda.From     = (double)control.GetValue(Canvas.LeftProperty);
                xda.To       = (moveInfo.DestX - width / 2);
                xda.Duration = new Duration(timeSpan);

                var yda = new DoubleAnimation();
                Storyboard.SetTarget(yda, control);
                Storyboard.SetTargetProperty(yda, new PropertyPath(Canvas.TopProperty));
                yda.From     = (double)control.GetValue(Canvas.TopProperty);
                yda.To       = (moveInfo.DestY - height / 2);
                yda.Duration = new Duration(timeSpan);
                storyboard.Children.Add(xda);
                storyboard.Children.Add(yda);

                storyboard.Completed += (sender, e) =>
                {
                    controlActionInfo.InAction = false;
                    this.RaiseEvent(new ActionEventArgs(MovingEndEvent, this)
                    {
                        Storyboard = storyboard, ActionInfo = moveInfo
                    });
                    HandleActionInfo(controlActionInfo);
                };
                this.RaiseEvent(new ActionEventArgs(MovingStartEvent, this)
                {
                    Storyboard = storyboard, ActionInfo = moveInfo
                });
                storyboard.Begin(this);
                controlActionInfo.InAction = true;
            }
        }
Пример #2
0
        private void HandleActionInfo(ControlActionInfo controlActionInfo)
        {
            if (controlActionInfo == null)
            {
                throw new ArgumentNullException("controlActionInfo");
            }

            if (controlActionInfo.ActionQueue.Count == 0)
            {
                return;
            }
            if (controlActionInfo.InAction)
            {
                return;
            }
            var actionInfo = controlActionInfo.ActionQueue.Dequeue();

            if (actionInfo is MoveInfoByPosition)
            {
                HandleMoveInfoByPosition(controlActionInfo, actionInfo as MoveInfoByPosition);
            }
        }