4

我有一个动画,当前在Control加载 a 时开始(动画本质上是一个等待微调器,应用于空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 工作。

4

2 回答 2

6

我创建了一个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>
于 2013-07-10T15:53:17.580 回答
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>
于 2013-07-10T15:49:06.153 回答