Reputation: 5882
I am trying to make a simple app where I bounce an image around the screen and rotate it. I want the rotation speed to be dynamic and change whenever the image hits one of the sides of the containing window. So, I was trying to figure out how to dynamically change the speed of the rotation and perhaps even the direction. I am experimenting with passing in a speed value and dynamically changing the rotation with an animation.
Anyway, I am trying to just test the ability to dynamically change the rotation by animating the AngleProperty and binding to the Angle in the XAML. I must be doing something wrong, because the image won't rotate.
Any help with this would be GREATLY appreciated !!
Thanks, Curtis
Here is my 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>
Here is my code-behind:
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);
}
}
}
Upvotes: 1
Views: 4387
Reputation: 5672
You're calling BeginAnimation()
on the Image, but using AngleProperty
from Bug
.
You could either use BeginAnimation()
on the RotateTransform
_image.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, animation);
or call BeginAnimation()
on your control:
this.BeginAnimation(AngleProperty, animation);
Upvotes: 3