I want to move car to point 0, 0 with moving animations, but the problem is that I get an error then I use this function - https://gyazo.com/1f47e18f955c12dc1907fcf4d7b19d6a. App look: https://gyazo.com/10fb071534809229cb98955f9fb86231. There is my code (I found this function in one article WPF. Easiest way to move Image to (X,Y) programmatically?):
public void Eiti(Image target, double newX, double newY)
{
var top = Canvas.GetTop(target);
var left = Canvas.GetLeft(target);
TranslateTransform trans = new TranslateTransform();
target.RenderTransform = trans;
DoubleAnimation anim1 = new DoubleAnimation(top, newY - top, TimeSpan.FromSeconds(10));
DoubleAnimation anim2 = new DoubleAnimation(left, newX - left, TimeSpan.FromSeconds(10));
trans.BeginAnimation(TranslateTransform.XProperty, anim1);
trans.BeginAnimation(TranslateTransform.YProperty, anim2);
}
and xaml:
<DockPanel Grid.Row="1" Background="#FF695887">
<Canvas>
<Image Name="car1" />
</Canvas>
</DockPanel>
How to solve it ?
public void Eiti(Image target, double newX, double newY)
{
var top = Canvas.GetTop(target);
var left = Canvas.GetLeft(target);
top = Double.IsNaN(top) ? 0 : top;
left = Double.IsNaN(left) ? 0 : left;
var storyboard = new Storyboard();
DoubleAnimation anim1 = new DoubleAnimation(top, newY, TimeSpan.FromSeconds(10));
Storyboard.SetTarget(anim1, target);
Storyboard.SetTargetProperty(anim1, new PropertyPath(Canvas.TopProperty));
storyboard.Children.Add(anim1);
DoubleAnimation anim2 = new DoubleAnimation(left, newX, TimeSpan.FromSeconds(10));
Storyboard.SetTarget(anim2, target);
Storyboard.SetTargetProperty(anim2, new PropertyPath(Canvas.LeftProperty));
storyboard.Children.Add(anim2);
storyboard.Begin();
}