WPF在XAML中实现持续动画的暂停、恢复、停止

1.动画通过EventTrigger监听按钮的FrameworkElement.Loaded事件,但控件载入时就进行动画,

   持续动画通过<BeginStoryboard Name="yourStoryName">下的<Storyboard>或者子动画的RepeatBehavior=Forever来实现,

   鼠标的移入移出,通过监听UIElement.MouseEnter和UIElement.MouseLeave事件,

 鼠标移入时,通过<PauseStoryboard BeginStoryboardName="yourStoryName"/>来暂停动画,

   鼠标移出时,通过<ResumeStoryboard BeginStoryboardName="yourStoryName"/>来恢复动画,具体代码如下:

<Button Content="Click Me">
    <Button.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Name="OpacityStoryboard">
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="(UIElement.Opacity)"
                        From="0" To="1" RepeatBehavior="Forever" AutoReverse="True" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="UIElement.MouseEnter">
            <PauseStoryboard BeginStoryboardName="OpacityStoryboard" />
        </EventTrigger>
        <EventTrigger RoutedEvent="UIElement.MouseLeave">
            <ResumeStoryboard BeginStoryboardName="OpacityStoryboard" />
        </EventTrigger>
    </Button.Triggers>
</Button>
View Code

   停止动画时,使用<StopStoryboard BeginStoryboardName="yourStoryName"/>

posted @ 2017-03-03 15:06  一菲聪天  阅读(7635)  评论(0编辑  收藏  举报