基于可见性停止/启动故事板动画

时间:2013-07-10 15:05:17

标签: wpf silverlight animation eventtrigger

我有一个当前加载Control时开始的动画(动画本质上是一个等待的微调器,应用于空ContentControl)。

然而动画会不断地占用资源。我想要的是根据动画控件是否可见来启动/停止动画,这可能吗?

<Canvas.Triggers>
    <EventTrigger RoutedEvent="ContentControl.Loaded">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation
                Storyboard.TargetName="SpinnerRotate"
                Storyboard.TargetProperty="Angle"
                From="0" To="360" Duration="0:0:01.3"
                RepeatBehavior="Forever" />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Canvas.Triggers>

我必须同时使用Silverlight和WPF。

2 个答案:

答案 0 :(得分:4)

可能不优雅,但有效。

<Border x:Name="square" Height="20" Width="20" Background="Aqua">
        <Border.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <BeginStoryboard>
                    <Storyboard x:Name="spinner">
                        <DoubleAnimation
                            Storyboard.TargetName="square"
                            Storyboard.TargetProperty="Opacity"
                            From="1" To="0" Duration="0:0:01.3"
                            AutoReverse="True"
                            RepeatBehavior="Forever" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Border.Triggers>
        <Border.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Source={RelativeSource Self}, Path=Visibility}" Value="{x:Static Visibility.Collapsed}">
                        <DataTrigger.EnterActions>
                            <StopStoryboard BeginStoryboardName="spinner"/>
                        </DataTrigger.EnterActions>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
    </Border>

答案 1 :(得分:4)

我创建了一个基于Ellipse属性旋转Visibility的示例。也许你可以使用这个。

 <Canvas>
    <Ellipse x:Name="Circle" Width="30" Height="30"
             Canvas.Left="50"
             Canvas.Top="50">
        <Ellipse.Fill>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black" Offset="0"/>
                <GradientStop Color="White" Offset="1"/>
            </LinearGradientBrush>
        </Ellipse.Fill>
        <Ellipse.RenderTransform>
            <RotateTransform x:Name="SpinnerRotate" CenterX="15" CenterY="15"/>
        </Ellipse.RenderTransform>
        <Ellipse.Style>
            <Style TargetType="Ellipse">
                <Style.Triggers>
                    <Trigger Property="Visibility" Value="Visible">
                        <Trigger.EnterActions>
                            <BeginStoryboard x:Name="SpinStoryboard">
                                <Storyboard >
                                    <DoubleAnimation Storyboard.TargetProperty="RenderTransform.Angle"
                                                     From="0" To="360" Duration="0:0:01.3"
                                                     RepeatBehavior="Forever" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <StopStoryboard BeginStoryboardName="SpinStoryboard"></StopStoryboard>
                        </Trigger.ExitActions>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Ellipse.Style>
    </Ellipse>
</Canvas>