我试图制作一个简单的应用程序,在屏幕上弹出一个图像并旋转它。我希望旋转速度是动态的,每当图像击中包含窗口的一侧时,就会发生变化。所以,我试图弄清楚如何动态地改变旋转的速度,甚至是方向。我正在尝试传递一个速度值,并用动画动态地改变旋转。
无论如何,我只是尝试测试通过动画AngleProperty和绑定到XAML中的角度来动态改变旋转的能力。我肯定做错了什么,因为图像不会旋转。
如有任何帮助,我们将不胜感激!
谢谢,柯蒂斯
这是我的XAML:
<UserControl x:Class="Scooter.Bug"
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"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Loaded="Bug_OnLoaded"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Image x:Name="_image" Source="Images/Author.png" RenderTransformOrigin="0.5, 0.5">
<Image.RenderTransform>
<RotateTransform Angle="{Binding Angle}"/>
</Image.RenderTransform>
</Image>
</Grid>
</UserControl>这是我的代码隐藏:
using System;
using System.Windows;
using System.Windows.Media.Animation;
namespace Scooter
{
public partial class Bug
{
public static readonly DependencyProperty SpinSpeedProperty = DependencyProperty.Register("SpinSpeed", typeof (TimeSpan), typeof (Bug), new PropertyMetadata(default(TimeSpan)));
public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof (double), typeof (Bug), new PropertyMetadata(default(double)));
public Bug()
{
InitializeComponent();
}
void _timer_Tick(object sender, EventArgs e)
{
Angle = Angle >= 360 ? 0 : Angle + 1;
}
public TimeSpan SpinSpeed
{
get { return (TimeSpan) GetValue(SpinSpeedProperty); }
set { SetValue(SpinSpeedProperty, value); }
}
public double Angle
{
get { return (double) GetValue(AngleProperty); }
set { SetValue(AngleProperty, value); }
}
private void Bug_OnLoaded(object sender, RoutedEventArgs e)
{
DoubleAnimation animation = new DoubleAnimation
{
From = 0,
To = 360,
RepeatBehavior = RepeatBehavior.Forever,
Duration = SpinSpeed
};
_image.BeginAnimation(AngleProperty, animation);
}
}
}发布于 2013-09-05 23:15:13
您正在调用图像上的BeginAnimation(),但使用的是来自Bug的AngleProperty。
您可以在BeginAnimation()上使用RotateTransform
_image.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, animation);或者调用控件上的BeginAnimation():
this.BeginAnimation(AngleProperty, animation);https://stackoverflow.com/questions/18647065
复制相似问题