使用代码来配置UIElement的Animation:
void OnButtonClick(object sender, RoutedEventArgs args) { Button btn = sender as Button; RotateTransform rotateTransform = btn.RenderTransform as RotateTransform; // Create and define animation DoubleAnimation anima = new DoubleAnimation(); anima.From = 0; anima.To = 360; anima.Duration = new Duration(TimeSpan.FromSeconds(0.5)); // Set attached properties Storyboard.SetTarget(anima, rotateTransform); Storyboard.SetTargetProperty(anima, new PropertyPath(RotateTransform.AngleProperty)); // Create storyboard, add animation, and fire it up! Storyboard storyboard = new Storyboard(); storyboard.Children.Add(anima); storyboard.Begin(); }
对于特定属性的Target设置:
Storyboard.SetTargetProperty(anima,
new PropertyPath("(Button.RenderTransform).(RotateTransform.Angle)"));
Animation的其他配置:
anima.RepeatBehavior = new RepeatBehavior(3);
anima.AutoReverse = true;
anima.FillBehavior = FillBehavior.HoldEnd;
XAML-Base的Animation:
<Storyboard x:Name="storyboard"> <DoubleAnimation Storyboard.TargetName="rotate" Storyboard.TargetProperty="Angle" From="0" To="360" Duration="0:0:0.5" /> </Storyboard>
另一种写法:
<DoubleAnimation Storyboard.TargetName="btn" Storyboard.TargetProperty="(Button.RenderTransform).(RotateTransform.Angle)" From="0" To="360" Duration="0:0:0.5" />
对于文本,有个特别的设置:TextOptions.TextHintingMode="Animated"
KeyFrames:
<Storyboard x:Name="jiggleStoryboard"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="translate" Storyboard.TargetProperty="X" RepeatBehavior="3x"> <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0" /> <LinearDoubleKeyFrame KeyTime="0:0:01" Value="-100" /> <LinearDoubleKeyFrame KeyTime="0:0:03" Value="100" /> <LinearDoubleKeyFrame KeyTime="0:0:04" Value="0" /> </DoubleAnimationUsingKeyFrames> </Storyboard>
Trigger:
<phone:PhoneApplicationPage.Triggers> <EventTrigger> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="TitlePanel" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:10" /> </Storyboard> </BeginStoryboard> </EventTrigger> </phone:PhoneApplicationPage.Triggers>
对于Attached属性的动画设置:
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ball" Storyboard.TargetProperty="(Canvas.Left)"> <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0" /> <LinearDoubleKeyFrame KeyTime="0:0:1" Value="400" /> <DiscreteDoubleKeyFrame KeyTime="0:0:2" Value="400" /> <LinearDoubleKeyFrame KeyTime="0:0:3" Value="0" /> <DiscreteDoubleKeyFrame KeyTime="0:0:4" Value="0" /> </DoubleAnimationUsingKeyFrames>
SplineKeyFrameExperiment:略
<SplineDoubleKeyFrame KeyTime="0:0:1" Value="10"
KeySpline="0.75 1, 0.4 0.8" />
PlaneProjection:
<TextBlock.Projection> <PlaneProjection x:Name="planeProjection"> </TextBlock.Projection> <Storyboard x:Name="rotateY"> <DoubleAnimation Storyboard.TargetName="planeProjection" Storyboard.TargetProperty="RotationY" From="0" To="360" Duration="0:0:5" /> </Storyboard> void RotateXClick(object sender, RoutedEventArgs args) { rotateX.Begin(); }
<phone:PhoneApplicationPage.Projection> <PlaneProjection x:Name="planeProjection" CenterOfRotationX="0" /> </phone:PhoneApplicationPage.Projection> <phone:PhoneApplicationPage.Triggers> <EventTrigger> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="planeProjection" Storyboard.TargetProperty="RotationY" From="-90" To="0" Duration="0:0:01" /> </Storyboard> </BeginStoryboard> </EventTrigger> </phone:PhoneApplicationPage.Triggers>