如何修复DoubleAnimation旋转图像?

时间:2019-08-07 08:34:16

标签: c# wpf xaml animation storyboard

我正在尝试为我创建的事件旋转EventHandler上的图像。事件处理程序工作得很好。但是图像不会旋转,我也不知道自己缺少什么。

此UserControl不是这样显示的,而是显示在另一个UserControl的网格中。

    <UserControl x:Class="Mabri.Module.P83.View.SchematicSystemView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Mabri.Module.P83.View"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
        <Image x:Name="drehteller" HorizontalAlignment="Center" VerticalAlignment="Center"/>


    </UserControl>

这是XAML的代码,起初我尝试将其放在separat类中,如果可能的话,它甚至会更好,但此刻我正试图使其运行。

  {

    private System.Windows.Controls.Image drehteller_image;
    private int currentAngle = 0;
    public SchematicSystemView()
    {
      EventAggregator.SubscribeEvent(this);
      InitializeComponent();
      drehteller_image = new System.Windows.Controls.Image()
      {
        Stretch = Stretch.Uniform,
        Source = new BitmapImage(new Uri("pack://application:,,,/Mabri.Module.P83;Component/Images/drehteller_unbestueckt.png")),
        RenderTransform = new RotateTransform()
      };
      drehteller.Source = drehteller_image.Source;

    }

    public void OnEventHandler(DrehtellerMoved e)
    {
      EventAggregator.PublishEvent(new LogInfo
      {
        Title = "Should rotate",
        Summary = "The drehteller should rotate now",
        Detail = "The drehteller should rotate " + currentAngle + " is the current angle",
        LogLevel = LogLevel.Information
      });

      int steps = e.steps;
      double timeforonestep = e.speed / e.steps;
      int angle = steps * 72;
      int angle_to_reach = currentAngle + angle;
      Storyboard storyboard = new Storyboard();
      storyboard.Duration = new Duration(TimeSpan.FromSeconds(timeforonestep * steps));
      DoubleAnimation rotateAnimation = new DoubleAnimation()
      {
        From = currentAngle,
        To = currentAngle + angle,
        Duration = storyboard.Duration
      };
      Storyboard.SetTarget(rotateAnimation, drehteller);
      Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
      storyboard.Children.Add(rotateAnimation);
      storyboard.Begin();
      currentAngle = angle_to_reach;
    }
  }

在测试案例中,e.steps等于1,e.speed等于10.0。

我希望图像可以旋转72度,但是由于某些原因,除了处理程序开头的LogMessage之外,什么都没有发生。

1 个答案:

答案 0 :(得分:1)

首先,将RotateTransform分配给Image的RenderTransform属性。否则就无法制作动画。

<Image x:Name="drehteller" HorizontalAlignment="Center" VerticalAlignment="Center"
       RenderTransformOrigin="0.5,0.5">
    <Image.RenderTransform>
        <RotateTransform/>
    </Image.RenderTransform>
</Image>

然后只设置From,而不是设置ToBy。拖放情节提要,然后直接在控件的RenderTransform上启动动画

var animation = new DoubleAnimation
{
    By = angle,
    Duration = TimeSpan.FromSeconds(...)
};

drehteller.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, animation);

最后,不要创建中间Image元素。那是多余的。

drehteller.Source = new BitmapImage(new Uri("..."));