在WPF中使用DoubleAnimationUsingKeyFrames为Slider制作动画

时间:2014-03-08 20:23:45

标签: wpf animation storyboard

我尝试做的应该相当简单,但无论出于何种原因,它都不起作用。 预期的行为应该是滑块拇指从0 => 100(silder min = 0 max = 100)开始,这意味着从左到右,然后再次返回,再次向右,并在那里等待5秒钟就是这样,喜欢KITT(骑士)。

到目前为止,我唯一取得的成就是它开始向右移动,就是它,停在那里,什么都不做,没有进一步的动画:

storyboard = new Storyboard();
storyboard.Completed += Storyboard_OnCompleted;

var doubleKeyFrame = new DoubleAnimationUsingKeyFrames();
doubleKeyFrame.KeyFrames.Add(new LinearDoubleKeyFrame(0, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0))));                       
doubleKeyFrame.KeyFrames.Add(new LinearDoubleKeyFrame(100, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 5))));
doubleKeyFrame.KeyFrames.Add(new LinearDoubleKeyFrame(0, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 5))));
doubleKeyFrame.KeyFrames.Add(new LinearDoubleKeyFrame(100, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 3))));
doubleKeyFrame.KeyFrames.Add(new LinearDoubleKeyFrame(100, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 5))));

Storyboard.SetTarget(doubleKeyFrame, slider);
Storyboard.SetTargetProperty(doubleKeyFrame, new PropertyPath("Value"));
storyboard.Children.Add(doubleKeyFrame);

storyboard.Begin(slider);

我现在正在玩那个小时,我不知道。也许有人可以帮我一点。

King问候,Bernhard。

1 个答案:

答案 0 :(得分:0)

试试这个,我为RepeatBehavior.Forever添加了doubleKeyFrame和持续时间:

var storyboard = new Storyboard();

var doubleKeyFrame = new DoubleAnimationUsingKeyFrames();
doubleKeyFrame.KeyFrames.Add(new LinearDoubleKeyFrame(100, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0))));
doubleKeyFrame.KeyFrames.Add(new LinearDoubleKeyFrame(0, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 5))));

doubleKeyFrame.KeyFrames.Add(new LinearDoubleKeyFrame(0, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0))));
doubleKeyFrame.KeyFrames.Add(new LinearDoubleKeyFrame(100, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 3))));

doubleKeyFrame.RepeatBehavior = RepeatBehavior.Forever;
doubleKeyFrame.Duration = TimeSpan.FromSeconds(10);

Storyboard.SetTarget(doubleKeyFrame, slider);
Storyboard.SetTargetProperty(doubleKeyFrame, new PropertyPath("Value"));
storyboard.Children.Add(doubleKeyFrame);

storyboard.Begin(slider);