在我的 WPF 应用程序中,我有一个包含一些 UserControl 对象的 Canvas 对象。
我希望在 Canvas 中为 UserControl 对象设置动画,DoubleAnimation
以便它们从 Canvas 的右侧移动到 Canvas 的左侧。到目前为止,我就是这样做的(通过将 UserControl 对象传递给函数):
private void Animate(FrameworkElement e)
{
DoubleAnimation ani = new DoubleAnimation()
{
From = _container.ActualWidth,
To = 0.0,
Duration = new Duration(new TimeSpan(0, 0, 10),
TargetElement = e
};
TranslateTransform trans = new TranslateTransform();
e.RenderTransform = trans;
trans.BeginAnimation(TranslateTransform.XProperty, ani, HandoffBehavior.Compose);
}
但是,这不允许我暂停动画,所以我考虑使用情节提要来代替,但我不知道如何实现这一点。到目前为止,这是我的尝试:
private void Animate(FrameworkElement e)
{
DoubleAnimation ani = new DoubleAnimation()
{
From = _container.ActualWidth,
To = 0.0,
Duration = new Duration(new TimeSpan(0, 0, 10),
TargetElement = e
};
Storyboard stb = new Storyboard();
Storyboard.SetTarget(ani, e);
Storyboard.SetTargetProperty(ani, "Left");
stb.Children.Add(ani);
stb.Begin();
}
当然,这会失败,因为 UserControl 没有Left
属性。我怎样才能实现我所追求的?
谢谢。