RepeatBehavior 结构

定义

描述 时间线 如何重复其简单持续时间。

/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
struct RepeatBehavior
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
public struct RepeatBehavior
Public Structure RepeatBehavior
<object property="iterationsx"/>
- or -
<object property="[days.]hours:minutes:seconds[.fractionalSeconds]"/>
- or -
<object property="Forever"/>
继承
RepeatBehavior
属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)

示例

此示例演示设置动画的 RepeatBehavior 的几种不同方法,以及这些设置如何影响动画。

<StackPanel Margin="20">
    <StackPanel.Resources>
        <Storyboard x:Name="myStoryboard">

            <!-- Create an animation that repeats indefinitely. -->
            <DoubleAnimation 
              Storyboard.TargetName="ForeverRepeatingTransform" 
              Storyboard.TargetProperty="ScaleX" 
              From="1" To="5" Duration="0:0:2" RepeatBehavior="Forever" />

            <!-- Create an animation that repeats for four seconds. Because
                 the animation is 2 seconds each, you get two repeats. -->
            <DoubleAnimation 
              Storyboard.TargetName="FourSecondsRepeatingTransform" 
              Storyboard.TargetProperty="ScaleX"
              From="1" To="5" Duration="0:0:2" RepeatBehavior="0:0:4" 
              EnableDependentAnimation="True"/>

            <!-- Create an animation that repeats twice. -->
            <DoubleAnimation 
              Storyboard.TargetName="TwiceRepeatingTransform" 
              Storyboard.TargetProperty="ScaleX" 
              From="1" To="5" Duration="0:0:2" RepeatBehavior="2x" 
              EnableDependentAnimation="True"/>

            <!-- Create an animation that repeats 0.5 times. The resulting animation
                 plays for one second, half of its Duration. It animates from 50 to 150. -->
            <DoubleAnimation 
              Storyboard.TargetName="HalfRepeatingTransform" 
              Storyboard.TargetProperty="ScaleX" 
              From="1" To="5" Duration="0:0:2" RepeatBehavior="0.5x" 
              EnableDependentAnimation="True"/>

            <!-- Create an animation that repeats for one second. The resulting animation
                 plays for one second, half of its Duration. It animates from 50 to 150. -->
            <DoubleAnimation 
              Storyboard.TargetName="OneSecondRepeatingTransform" 
              Storyboard.TargetProperty="ScaleX" 
              From="1" To="5" Duration="0:0:2" RepeatBehavior="0:0:1" 
              EnableDependentAnimation="True"/>
        </Storyboard>
    </StackPanel.Resources>

    <!-- Create several rectangles to animate. -->
    <Rectangle Fill="Red" Width="50" Height="20" >
        <Rectangle.RenderTransform>
            <ScaleTransform x:Name="ForeverRepeatingTransform" />
        </Rectangle.RenderTransform>
   </Rectangle>
   <Rectangle Fill="Blue" Width="50" Height="20" >
       <Rectangle.RenderTransform>
           <ScaleTransform x:Name="FourSecondsRepeatingTransform" />
       </Rectangle.RenderTransform>
   </Rectangle>
   <Rectangle Fill="Yellow" Width="50" Height="20" >
       <Rectangle.RenderTransform>
           <ScaleTransform x:Name="TwiceRepeatingTransform" />
       </Rectangle.RenderTransform>
   </Rectangle>
   <Rectangle Fill="Green" Width="50" Height="20" >
       <Rectangle.RenderTransform>
           <ScaleTransform x:Name="HalfRepeatingTransform" />
       </Rectangle.RenderTransform>
   </Rectangle>
   <Rectangle Fill="Orange" Width="50" Height="20" >
       <Rectangle.RenderTransform>
           <ScaleTransform x:Name="OneSecondRepeatingTransform" />
       </Rectangle.RenderTransform>
   </Rectangle>

        <!-- Create buttons to restart and stop the animations. -->
   <Button Margin="10" Content="Restart Animation" Click="Start_Animation" />


</StackPanel>
private void Start_Animation(object sender, RoutedEventArgs e)
{
    myStoryboard.Begin();
}

此示例演示如何在代码中设置 RepeatBehavior 。 动画与上一示例中的动画相同,但设置了 x:Name 属性,并且 RepeatBehavior 在 方法中 Start_Animation 设置,而不是在 XAML 中设置。

<Storyboard x:Name="myStoryboard">

    <!-- Create an animation that repeats indefinitely. -->
    <DoubleAnimation x:Name="ForeverRepeatingAnimation"
                     Storyboard.TargetName="ForeverRepeatingTransform" 
                     Storyboard.TargetProperty="ScaleX" 
                     From="1" To="5" Duration="0:0:2"  />

    <!-- Create an animation that repeats for four seconds. Because 
        the animation is 2 seconds each, you get two repeats. -->
    <DoubleAnimation x:Name="FourSecondsRepeatingAnimation"
                     Storyboard.TargetName="FourSecondsRepeatingTransform" 
                     Storyboard.TargetProperty="ScaleX"
                     From="1" To="5" Duration="0:0:2"  
                     EnableDependentAnimation="True"/>

    <!-- Create an animation that repeats twice. -->
    <DoubleAnimation x:Name="TwiceRepeatingAnimation"
                     Storyboard.TargetName="TwiceRepeatingTransform" 
                     Storyboard.TargetProperty="ScaleX" 
                     From="1" To="5" Duration="0:0:2"  
                     EnableDependentAnimation="True"/>
</Storyboard>
private void Start_Animation(object sender, RoutedEventArgs e)
{
    // Set RepeatBehavior of Forever.
    var repeatBehavior = new RepeatBehavior();
    repeatBehavior.Type = RepeatBehaviorType.Forever;
    ForeverRepeatingAnimation.RepeatBehavior = repeatBehavior;

    // Set RepeatBehavior with Duration of 4 seconds.
    FourSecondsRepeatingAnimation.RepeatBehavior = new RepeatBehavior(new TimeSpan(0, 0, 4));

    // Set RepeatBehavior with Count of 2.
    TwiceRepeatingAnimation.RepeatBehavior = new RepeatBehavior(2);

    myStoryboard.Begin();
}

注解

有三种类型的 RepeatBehavior 行为:

  • 时间跨度:指定 时间线的活动持续时间,如果 Timeline.Duration 较短,可能会重复动画。 例如 ,具有简单Timeline.Duration 值为 1 秒、 RepeatBehavior.Duration 值为 2.5 秒的时间线将运行 2.5 次迭代和 2.5 秒。
  • 迭代计数:指定 时间线 的简单持续时间播放的次数。 默认迭代计数为 1.0,这意味着 时间线 在其简单持续时间之一内处于活动状态。 计数 0.5 指定时间线在其简单持续时间的一半内处于活动状态,而计数 2 指定时间线重复其简单持续时间两次。 有关详细信息,请参阅 Count
  • 永久: 时间线 无限期重复。

RepeatBehavior 应仅包含其两个可能的数据属性 CountDuration 中的一个的非零值。 如果 RepeatBehaviorTypeCount,则 RepeatBehavior 的 Count 成员是相关值。 如果 RepeatBehaviorTypeDuration,则 RepeatBehavior 的 Duration 成员是相关值。 如果 RepeatBehaviorTypeForever,则 CountDuration 都不相关;重复行为使目标动画连续重复,没有限制。

有关 XAML 语法的说明

不能在 ResourceDictionary 中将 RepeatBehavior 声明为可共享对象。

RepeatBehavior 的投影和成员

如果使用 Microsoft .NET 语言 (C# 或 Microsoft Visual Basic) ,则 RepeatBehavior 具有可用的非数据成员,并且其数据成员 CountDurationType 公开为读写属性,而不是字段。

如果使用 Visual C++ 组件扩展 (C++/CX) ,则 RepeatBehavior 具有可用的非数据成员,并且其数据成员 CountDurationType 公开为只读属性,而不是字段。

如果使用 Windows 运行时 模板库 (WRL) 使用 C++ 进行编程,则只有数据成员字段 CountDurationType 作为 RepeatBehavior 的成员存在,并且不能使用成员表中列出的实用工具方法或属性。 WRL 代码可以访问 RepeatBehaviorHelper 类上存在的类似实用工具方法。

字段

Count

时间线应重复的次数。

Duration

时间线应重复的时间跨度。

Type

此实例表示的重复行为的模式或类型,作为 枚举的值。

适用于