WPF 格式的图像双重动画
Double animations with Image in WPF form
我想用移动动画将汽车移动到点 0, 0,但问题是我得到一个错误然后我使用这个函数 - https://gyazo.com/1f47e18f955c12dc1907fcf4d7b19d6a。
应用外观:
https://gyazo.com/10fb071534809229cb98955f9fb86231。
这是我的代码(我在一篇文章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);
}
和xaml:
<DockPanel Grid.Row="1" Background="#FF695887">
<Canvas>
<Image Name="car1" />
</Canvas>
</DockPanel>
如何解决?
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();
}
我想用移动动画将汽车移动到点 0, 0,但问题是我得到一个错误然后我使用这个函数 - https://gyazo.com/1f47e18f955c12dc1907fcf4d7b19d6a。 应用外观: https://gyazo.com/10fb071534809229cb98955f9fb86231。 这是我的代码(我在一篇文章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);
}
和xaml:
<DockPanel Grid.Row="1" Background="#FF695887">
<Canvas>
<Image Name="car1" />
</Canvas>
</DockPanel>
如何解决?
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();
}