コード例 #1
0
        private static Storyboard CreateStoryboard(this DependencyObject target, DependencyProperty animatingDependencyProperty, string propertyPath, string propertyKey, ref object toValue, TimeSpan durationTimeSpan, IEasingFunction easingFunction)
        {
            object     obj        = target.GetValue(animatingDependencyProperty);
            Storyboard storyboard = new Storyboard();

            Storyboard.SetTarget((DependencyObject)storyboard, target);
            Storyboard.SetTargetProperty((DependencyObject)storyboard, new PropertyPath(propertyPath, new object[0]));
            if (obj != null && toValue != null)
            {
                double doubleValue1;
                double doubleValue2;
                if (ValueHelper.TryConvert(obj, out doubleValue1) && ValueHelper.TryConvert(toValue, out doubleValue2))
                {
                    DoubleAnimation doubleAnimation = new DoubleAnimation();
                    doubleAnimation.Duration = (Duration)durationTimeSpan;
                    doubleAnimation.To       = new double?(ValueHelper.ToDouble(toValue));
                    toValue = (object)doubleAnimation.To;
                    storyboard.Children.Add((Timeline)doubleAnimation);
                }
                else
                {
                    DateTime dateTimeValue1;
                    DateTime dateTimeValue2;
                    if (ValueHelper.TryConvert(obj, out dateTimeValue1) && ValueHelper.TryConvert(toValue, out dateTimeValue2))
                    {
                        ObjectAnimationUsingKeyFrames animationUsingKeyFrames = new ObjectAnimationUsingKeyFrames();
                        animationUsingKeyFrames.Duration = (Duration)durationTimeSpan;
                        long count = (long)(durationTimeSpan.TotalSeconds * 20.0);
                        if (count < 2L)
                        {
                            count = 2L;
                        }
                        IEnumerable <TimeSpan> intervalsInclusive = ValueHelper.GetTimeSpanIntervalsInclusive(durationTimeSpan, count);
                        foreach (DiscreteObjectKeyFrame discreteObjectKeyFrame in Enumerable.Zip <DateTime, TimeSpan, DiscreteObjectKeyFrame>(ValueHelper.GetDateTimesBetweenInclusive(dateTimeValue1, dateTimeValue2, count), intervalsInclusive, (Func <DateTime, TimeSpan, DiscreteObjectKeyFrame>)((dateTime, timeSpan) =>
                        {
                            return(new DiscreteObjectKeyFrame()
                            {
                                Value = (object)dateTime,
                                KeyTime = (KeyTime)timeSpan
                            });
                        })))
                        {
                            animationUsingKeyFrames.KeyFrames.Add((ObjectKeyFrame)discreteObjectKeyFrame);
                            toValue = discreteObjectKeyFrame.Value;
                        }
                        storyboard.Children.Add((Timeline)animationUsingKeyFrames);
                    }
                }
            }
            if (storyboard.Children.Count == 0)
            {
                ObjectAnimationUsingKeyFrames animationUsingKeyFrames = new ObjectAnimationUsingKeyFrames();
                DiscreteObjectKeyFrame        discreteObjectKeyFrame1 = new DiscreteObjectKeyFrame();
                discreteObjectKeyFrame1.Value   = toValue;
                discreteObjectKeyFrame1.KeyTime = (KeyTime) new TimeSpan(0, 0, 0);
                DiscreteObjectKeyFrame discreteObjectKeyFrame2 = discreteObjectKeyFrame1;
                animationUsingKeyFrames.KeyFrames.Add((ObjectKeyFrame)discreteObjectKeyFrame2);
                storyboard.Children.Add((Timeline)animationUsingKeyFrames);
            }
            return(storyboard);
        }