后台动态调用创建平移动画

        public void SetSb(UIElement element, double fromPosition, double toPosition)
        {
            Storyboard sb = new Storyboard();

            Duration duration = new Duration(TimeSpan.FromSeconds(0.5));
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.Duration = duration;
            doubleAnimation.From = 0;
            doubleAnimation.To = 1;
            sb.Children.Add(doubleAnimation);
            Storyboard.SetTarget(doubleAnimation, element);
            Storyboard.SetTargetProperty(doubleAnimation,new PropertyPath("(UIElement.Opacity)"));

            DoubleAnimationUsingKeyFrames doubleAnimationUsingKeyFrames = new DoubleAnimationUsingKeyFrames();
            SplineDoubleKeyFrame splineDoubleKeyFrame1 = new SplineDoubleKeyFrame();
            splineDoubleKeyFrame1.KeyTime = new TimeSpan(0, 0, 0, 0);
            splineDoubleKeyFrame1.Value = fromPosition;

            SplineDoubleKeyFrame splineDoubleKeyFrame2 = new SplineDoubleKeyFrame();
            splineDoubleKeyFrame2.KeyTime = new TimeSpan(0, 0, 0, 0, 300);
            splineDoubleKeyFrame2.Value = toPosition;

            doubleAnimationUsingKeyFrames.KeyFrames.Add(splineDoubleKeyFrame1);
            doubleAnimationUsingKeyFrames.KeyFrames.Add(splineDoubleKeyFrame2);

            sb.Children.Add(doubleAnimationUsingKeyFrames);
            Storyboard.SetTarget(doubleAnimationUsingKeyFrames, element);
            Storyboard.SetTargetProperty(doubleAnimationUsingKeyFrames, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)"));

            sb.Completed += new EventHandler(sb_Completed);

            sb.Begin();


        }

posted @ 2011-11-20 01:02  therockthe  阅读(321)  评论(1)    收藏  举报