假如要实现这样一种控件,它的透明度由0平滑过渡到1,再由1平滑过渡到0(也就是忽明忽暗的闪烁效果)。
一种方法是定义一个定时器,重复的改变它的透明度。
一种方法则是可以通过storyboard来实现这种闪烁效果,代码实现起来更为简洁,代码如下:
- private Storyboard PrepareShowStory()
- {
- Storyboard story = new Storyboard();
- DoubleAnimation animation;
- animation = new DoubleAnimation();
- animation.From = 0;
- animation.To = 1;
- animation.Duration = new Duration(TimeSpan.FromMilliseconds(Duration));
- Storyboard.SetTarget(animation, MyTestImage);
- Storyboard.SetTargetProperty(animation, new PropertyPath("(UIElement.Opacity)"));
- story.Children.Add(animation);
- story.AutoReverse = true;
- story.RepeatBehavior = RepeatBehavior.Forever;
- return story;
- }
- m_StoryBoard = PrepareShowStory();
- m_StoryBoard.Begin();
要暂停动画,可以这样:
- m_StoryBoard.Pause();
要重新运行动画,则可以:
- m_StoryBoard.Resume();
要结束动画,则可以:
- m_StoryBoard.Stop();