Wpf 如何使用故事板旋转图像
我正在尝试旋转一个图像,并希望在下一阶段中使用故事板,我需要添加多个动画运行的时间 我的代码位于按钮的单击事件中,如下所示:Wpf 如何使用故事板旋转图像,wpf,animation,Wpf,Animation,我正在尝试旋转一个图像,并希望在下一阶段中使用故事板,我需要添加多个动画运行的时间 我的代码位于按钮的单击事件中,如下所示: //start the animation DoubleAnimation animationRotation = new DoubleAnimation(); animationRotation.From = -17; animationRotation.To = 17;
//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>
我做错了什么?您发布的代码似乎没有问题,但是,路径引用了现有元素,因此请确保在xaml中包含了相应的节点,其中定义了
Arm
控件
假设Arm
是一个矩形
控件。然后,您的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
、TransformGroup
和RotateTransform
节点,因此,动画有一些现有元素需要更改,如代码中的属性路径中所指定的。在与新项目和简化的代码进行了大量的混淆之后,我在另一个线程中提出的问题略有不同,有人发布了工作代码
在调查中,我的动画不起作用的原因是我为动画提供了持续时间,而不是故事板本身!!doh
完整答案(这是我在这个话题上收到的最完整的答案之一)
卢卡斯,谢谢你在这个问题上的耐心和帮助
Dan我检查了XAML,它定义了转换节点(现在包含在上面的文章中供参考的是完整的XAML)。代码和XAML仍在编译,但动画未运行(还在代码中添加了一个断点,并见证了它的启动和命中事件ok。我已在c#中复制了您的代码(在您更新帖子之前),将其添加到示例xaml中,并成功运行。我不确定我是否正确理解您的意思,但您将动画创建板放入路由事件中,并检查它是否在您的控制范围内触发,对吗?现在我没有时间这样做,但稍后我将尝试找出问题所在。就目前而言,xaml与发布在d代码在C#模块中的一个函数中,该函数由窗口高亮显示的鼠标向下触发。该函数启动正常,我可以单步执行代码,但最后动画没有运行…唯一需要注意的是,整个内容是用户控件的一部分,但这不重要…那么…代码隐藏中的代码在控件的代码或它是否在其他地方使用(我可以看到您可以访问Arm
图像,但您可能会在控制之外获得它并引用它,所以我请求只是为了确保)。