1

我正在尝试旋转图像并想使用情节提要,因为在下一阶段我需要添加多个运行时间的动画。

我的代码在按钮的单击事件中,如下所示:

//start the animation
            DoubleAnimation animationRotation = new DoubleAnimation();
    
            animationRotation.From = -17;
            animationRotation.To = 17;
            animationRotation.Duration = new Duration(TimeSpan.FromMilliseconds(NumericDisplay.Milliseconds));
            animationRotation.RepeatBehavior = RepeatBehavior.Forever;
            animationRotation.AccelerationRatio = 0.3;
            animationRotation.DecelerationRatio = 0.3;
            animationRotation.AutoReverse = true;
            
            Storyboard storyboard = new Storyboard();

            Storyboard.SetTarget(animationRotation, Arm);
            Storyboard.SetTargetProperty(animationRotation,
              new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(RotateTransform.Angle)")); 

            storyboard.Children.Add(animationRotation);

            // Add the storyboard to the tracking collection.      
            //this.Stostoryboards.Add(bomb, storyboard);
            
            // Configure and start the storyboard.
            this.BeginStoryboard(storyboard);

代码编译没有警告但点击事件没有启动动画,

编辑 建议的答案之一要求我检查 XAML 包括旋转转换的定义......下面的 XAML 是我正在使用的

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <ImageBrush x:Key="ImageBrush_Decrement" ImageSource="Images/pad-metronome-decrement-button.png" Stretch="Fill"/>
        <ImageBrush x:Key="ImageBrush_Increment" ImageSource="Images/pad-metronome-increment-button.png" Stretch="Fill"/>
        
    </ResourceDictionary>
</UserControl.Resources>
<UserControl.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        
    </EventTrigger>
</UserControl.Triggers>

<Grid x:Name="LayoutRoot" Height="412">
    <Image x:Name="MetronomeWindowBackground" Height="140" Margin="237,1.5,231,0" Source="Images\pad-metronome-top-under-bg.png" Stretch="Fill" VerticalAlignment="Top"/>
    <Image x:Name="Arm" Margin="506,17,493,0" Source="Images\pad-metronome-arm.png" Stretch="Fill" Height="326" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5">
        <Image.RenderTransform>
            <TransformGroup>
                <RotateTransform/>
                
            </TransformGroup>
        </Image.RenderTransform>
    </Image>
    <Image x:Name="MetronomeFlash" Height="209" Margin="104,0,96,0" Source="Images\pad-metronome-flash-top-landscape.png" Stretch="Fill" VerticalAlignment="Top" d:IsHidden="True" />
    <Image x:Name="MetronomeBackground" Height="209" Source="Images\pad-metronome-top-bg-landscape.png" Stretch="Fill" VerticalAlignment="Top" Margin="3,0,-3,0"/>
    <Image x:Name="MetronomeStartButton" Margin="379.5,100.5,373.5,0" Source="Images\pad-metronome-start-button-base.png" Stretch="Fill" Height="110" VerticalAlignment="Top"/>
    <Image x:Name="MetronomeTapPadLeft" HorizontalAlignment="Left" Height="209" Margin="5,1.5,0,0" Source="Images\pad-metronome-tap-pad-left.png" Stretch="Fill" VerticalAlignment="Top" Width="136"/>
    <Image x:Name="MetronomeTapPadRight" HorizontalAlignment="Right" Source="Images\pad-metronome-tap-pad-right.png" Stretch="Fill" Width="136" Height="209" VerticalAlignment="Top"/>
    <Image x:Name="MetronomeWindowHighlight" Height="105" Margin="238.5,18,231.5,0" Source="Images\pad-metronome-window-overlay.png" Stretch="Fill" VerticalAlignment="Top"/>
    <Image x:Name="MetronomeBottomBackground" Margin="3,208,-3,362" Source="Images\pad-metronome-section-bottom-bg.png" Stretch="Fill"/>
    <my:MetronomeLargeNumericDisplay HorizontalAlignment="Left" Margin="459,120,0,0" x:Name="NumericDisplay" VerticalAlignment="Top" Value="999" Width="122.25" />
    <Image Height="78" Margin="448,110.5,436,0" Source="Images\pad-metronome-start-button-overlay.png" Stretch="Fill" VerticalAlignment="Top"
           x:Name="DisplayOverlay" MouseDown="DisplayOverlay_MouseDown" />
    <RepeatButton x:Name="ButtonDecrement" Content="" BorderThickness="7" HorizontalAlignment="Left" Margin="252,110.5,0,0" VerticalAlignment="Top" Width="149" Height="100" Background="{DynamicResource ImageBrush_Decrement}" Style="{DynamicResource RepeatButtonStyle_noflash}" BorderBrush="{DynamicResource ImageBrush_Decrement}" d:LayoutOverrides="HorizontalAlignment" Click="ButtonDecrement_Click"></RepeatButton>
    <RepeatButton Content="" BorderThickness="7" HorizontalAlignment="Left" Margin="631,110.5,0,0" VerticalAlignment="Top" Width="149" Height="100" Background="{DynamicResource ImageBrush_Increment}" Style="{DynamicResource RepeatButtonStyle_noflash}" BorderBrush="{DynamicResource ImageBrush_Decrement}" Name="ButtonIncrement" Click="ButtonIncrement_Click" />
</Grid>

我究竟做错了什么?

4

2 回答 2

0

您发布的代码似乎没问题,但是,路径引用了现有元素,因此请确保您已在Arm定义控件的 xaml 中包含适当的节点。

假设Arm是一个Rectangle控件。然后,您的 xaml 应该或多或少像这样:

...
<Rectangle x:Name="Arm" Fill="Aqua" Width="100" Height="100" Canvas.Left="100" Canvas.Top="100">
    <Rectangle.RenderTransform>
        <TransformGroup>
            <RotateTransform />
        </TransformGroup>
    </Rectangle.RenderTransform>
</Rectangle>
...

Arm可以是另一种类型的控件,但重要的是RenderTransform,TransformGroupRotateTransform节点已定义,因此动画有一些现有元素需要更改,如PropertyPath代码中指定的那样。

于 2012-06-15T21:15:44.660 回答
0

在对新项目和大量简化代码进行了很多处理之后,我在另一个线程中提出的问题略有不同,并且有人发布了工作代码。

经过调查,我的动画无法正常工作的原因是我为动画提供了持续时间,而不是故事板本身!多哈。

完整答案在这里(这是我收到的关于这个主题的最完整的答案之一)

如何仅使用 c# 代码在图像对象上创建旋转动画(在 WPF 窗口内)

卢卡斯,感谢您在这个问题上的耐心和帮助。

于 2012-06-18T06:50:32.307 回答