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 的計數則指定時間軸重複其簡單持續時間兩次。 如需詳細資訊,請參閱 計數
  • 永遠: 時間軸 會無限期地重複。

RepeatBehavior 應該只包含兩個可能資料屬性 CountDuration的其中一個非零值。 如果 RepeatBehaviorTypeCount,則 RepeatBehavior 的 Count 成員是相關值。 如果 RepeatBehaviorTypeDuration,則 RepeatBehavior 的 Duration 成員是相關值。 如果 RepeatBehaviorTypeForever,則 CountDuration 都無關;重複行為使得目標動畫會連續重複,而不會有限制。

XAML 語法的注意事項

您無法將 RepeatBehavior 宣告為 ResourceDictionary中的可共用物件。

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

這個實例代表的模式或重複行為類型,做為列舉值。

適用於