WPF在XAML中旋转矩形动画

时间:2012-09-06 10:28:03

标签: wpf xaml animation autorotate

如何无限旋转矩形 - 仅在xaml定义中。到目前为止,我找到了一个代码但没有xaml的解决方案: http://www.codeproject.com/Articles/23257/Beginner-s-WPF-Animation-Tutorial 我这样用的是:

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

但是我怎样才能用XAML实现这个目标呢?

1 个答案:

答案 0 :(得分:52)

像这样的东西

<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触发器并随时运行此故事板。