共用方式為


HOW TO:使用主要畫面格建立物件的動畫

本範例說明如何使用主要畫面格建立物件的動畫,本範例的物件是 Page 控制項的 Background 屬性。

範例

下列範例使用 ObjectAnimationUsingKeyFrames 類別,以動畫方式顯示 Page 控制項之 Background 屬性的色彩變更。 範例動畫會定期變更為不同的背景筆刷。 本動畫使用 DiscreteObjectKeyFrame 類別以建立三個不同的主要畫面格。 這個動畫以下列方式使用主要畫面格:

  1. 在第一秒結束時,以動畫方式呈現 LinearGradientBrush 類別的執行個體。 範例的這一部分會將線形漸層套用至背景色彩,色彩就會由黃色轉換為橘色再轉換為紅色。

  2. 在下一秒結束時,以動畫方式呈現 RadialGradientBrush 類別的執行個體。 範例的這一部分會將放射狀漸層套用至背景色彩,色彩就會由白色轉換為藍色再轉換為黑色。

  3. 在第三秒結束時,以動畫方式呈現 DrawingBrush 類別的執行個體。 範例的這一部分會將棋盤式樣式套用至背景。

  4. 動畫又再開始並且不斷重複。

注意事項注意事項

DiscreteObjectKeyFrame 是您唯一能搭配 ObjectAnimationUsingKeyFrames 類別使用的主要畫面格。像是 DiscreteObjectKeyFrame 這類的主要畫面格會忽然變更值,也就是說,本範例中的色彩會忽然改變。

<Page 
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" >
    <Page.Triggers>
      <EventTrigger RoutedEvent="Page.Loaded">
        <BeginStoryboard>
          <Storyboard>

            <!-- ObjectAnimationUsingKeyFrames is used to animate properties that take
                 an object as a value. This animation lasts for 4 seconds using 3 KeyFrames which
                 swap different brush objects at regular intervals, making the background of the Page
                 change. -->
            <ObjectAnimationUsingKeyFrames
              Storyboard.TargetProperty="Background"
              Duration="0:0:4" RepeatBehavior="Forever">
            <ObjectAnimationUsingKeyFrames.KeyFrames>

              <!-- Note: Only discrete interpolation (DiscreteObjectKeyFrame) is available for 
              use with ObjectAnimationUsingKeyFrames which merely swaps objects according to
              a specified timeline. Other types of interpolation are too problematic to apply
              to objects.  -->

              <!-- Using a DiscreteObjectKeyFrame, the Page Background suddenly changes 
                   to a LinearGradientBrush after the first second of the animation. -->
              <DiscreteObjectKeyFrame KeyTime="0:0:1">
                <DiscreteObjectKeyFrame.Value>
                  <LinearGradientBrush>
                    <LinearGradientBrush.GradientStops>
                      <GradientStop Color="Yellow" Offset="0.0" />
                      <GradientStop Color="Orange" Offset="0.5" />
                      <GradientStop Color="Red" Offset="1.0" />
                    </LinearGradientBrush.GradientStops>
                  </LinearGradientBrush>
                </DiscreteObjectKeyFrame.Value>
              </DiscreteObjectKeyFrame>

              <!-- Using a DiscreteObjectKeyFrame, the Page Background suddenly changes 
                   to a RadialGradientBrush after the second second of the animation. -->
              <DiscreteObjectKeyFrame KeyTime="0:0:2">
                <DiscreteObjectKeyFrame.Value>
                  <RadialGradientBrush GradientOrigin="0.75,0.25">
                    <RadialGradientBrush.GradientStops>
                      <GradientStop Color="White" Offset="0.0" />
                      <GradientStop Color="MediumBlue" Offset="0.5" />
                      <GradientStop Color="Black" Offset="1.0" />
                    </RadialGradientBrush.GradientStops>
                  </RadialGradientBrush>
                </DiscreteObjectKeyFrame.Value>
              </DiscreteObjectKeyFrame>

              <!-- Using a DiscreteObjectKeyFrame, the Page Background suddenly 
                   changes to a DrawingBrush (creates a checkerboard pattern) after the 
                   third second of the animation. -->
              <DiscreteObjectKeyFrame KeyTime="0:0:3">
                <DiscreteObjectKeyFrame.Value>
                  <DrawingBrush Viewport="0,0,0.25,0.25" TileMode="Tile">
                    <DrawingBrush.Drawing>
                      <DrawingGroup>
                        <DrawingGroup.Children>
                          <GeometryDrawing Brush="White">
                            <GeometryDrawing.Geometry>
                              <RectangleGeometry Rect="0,0,1,1" />
                            </GeometryDrawing.Geometry>
                          </GeometryDrawing>
                          <GeometryDrawing Brush="Black"
                           Geometry="M 0,0 L0,0.5 0.5,0.5 0.5,1 1,1 1,0.5 0.5,0.5 0.5,0" />
                        </DrawingGroup.Children>
                      </DrawingGroup>
                    </DrawingBrush.Drawing>
                  </DrawingBrush>
                </DiscreteObjectKeyFrame.Value>
              </DiscreteObjectKeyFrame>          
            </ObjectAnimationUsingKeyFrames.KeyFrames>
          </ObjectAnimationUsingKeyFrames>        
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Page.Triggers>
</Page>

如需完整範例,請參閱 KeyFrame 動畫範例 (英文)。

請參閱

參考

ObjectAnimationUsingKeyFrames

Background

Page

DiscreteObjectKeyFrame

LinearGradientBrush

RadialGradientBrush

DrawingBrush

概念

主要畫面格動畫概觀

其他資源

主要畫面格動畫 HOW TO 主題