很多时候,blend在制作动画时,会自动为对象生成如下代码:
代码语言:js复制<Rectangle Fill="White" Stroke="Black" HorizontalAlignment="Center" VerticalAlignment="Center" Width="300" Height="30" RenderTransformOrigin="0.5,0.5" x:Name="rect">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
如果想在cs代码中动态控制,就得先知道如何引用它们,在不借助x:Name的情况下,可以这么做:
代码语言:js复制private void Button_Click(object sender, RoutedEventArgs e)
{
RotateTransform r = (rect.RenderTransform as TransformGroup).Children[2] as RotateTransform;
DoubleAnimation dblAnim = new DoubleAnimation() { Duration = TimeSpan.FromSeconds(1), To = 135.0, EasingFunction = new BackEase() { EasingMode = EasingMode.EaseInOut } };
Storyboard.SetTarget(dblAnim, r);
Storyboard.SetTargetProperty(dblAnim, new PropertyPath(RotateTransform.AngleProperty));
Storyboard sb = new Storyboard();
sb.Children.Add(dblAnim);
sb.AutoReverse = true;
sb.Begin();
}
}
上面的代码,将生成一段动画:1秒钟内将矩形旋转135度