35

如何定义 XAML 以无限旋转矩形?

到目前为止,我找到了一个带有代码但没有 xaml 的解决方案:http: //www.codeproject.com/Articles/23257/Beginner-s-WPF-Animation-Tutorial 我这样使用:

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
    var doubleAnimation = new DoubleAnimation(360, 0, new Duration(TimeSpan.FromSeconds(1)));
    var rotateTransform = new RotateTransform();
    
    rect1.RenderTransform = rotateTransform;
    rect1.RenderTransformOrigin = new Point(0.5, 0.5);
    doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
    
    rotateTransform.BeginAnimation(RotateTransform.AngleProperty, doubleAnimation);
}

但是我怎样才能只用 XAML 来实现呢?

4

1 回答 1

70

像这样的东西

<Rectangle x:Name="rect1" RenderTransformOrigin="0.5, 0.5">
  <Rectangle.RenderTransform>
    <!-- giving the transform a name tells the framework not to freeze it -->
    <RotateTransform x:Name="noFreeze" />
  </Rectangle.RenderTransform>
  <Rectangle.Triggers>
    <EventTrigger RoutedEvent="Loaded">
      <BeginStoryboard>
        <Storyboard>
          <DoubleAnimation
            Storyboard.TargetProperty="(Rectangle.RenderTransform).(RotateTransform.Angle)"
            To="-360" Duration="0:0:1" RepeatBehavior="Forever" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Rectangle.Triggers>
</Rectangle>

当然,您可以随时删除Loaded触发器并运行此情节提要。

于 2012-09-06T10:43:10.887 回答