WPF Rotate rectangle animation in XAML

一个人想着一个人 提交于 2019-12-20 10:26:52

问题


How can I rotate a rectangle infinitely - ONLY within xaml definition. So far I found a solution with code but no xaml: http://www.codeproject.com/Articles/23257/Beginner-s-WPF-Animation-Tutorial which I use like this:

    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);
    }

But how can I achieve this with XAML only?


回答1:


Something like this

<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>

Of course you can remove Loaded trigger and run this storyboard whenever you want.



来源:https://stackoverflow.com/questions/12298019/wpf-rotate-rectangle-animation-in-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!