我有以下有效的方法。我想把它放在一个返回故事板的实用方法中。我将其转换为故事板的每一次尝试都失败了,我花了很多时间进行研究。除非有人来救我,否则我准备放弃。
这是我要转换的代码:
public override void Begin(FrameworkElement element, int duration)
{
var transform = new ScaleTransform();
element.LayoutTransform = transform;
var animation = new DoubleAnimation
{
From = 1,
To = 0,
Duration = TimeSpan.FromMilliseconds(duration),
FillBehavior = FillBehavior.Stop,
EasingFunction = new QuinticEase { EasingMode = EasingMode.EaseIn }
};
transform.BeginAnimation(ScaleTransform.ScaleXProperty, animation);
transform.BeginAnimation(ScaleTransform.ScaleYProperty, animation);
}
因此,我想返回一个 Storyboard,而不是两个 BeginAnimation() 调用,所以我所要做的就是调用 storyboard.Begin()。我知道这应该不难做到,但我就是不明白。
谢谢。
编辑:响应 HB 的建议,我尝试了以下代码,但仍然无法正常工作:
private static Storyboard CreateAnimationStoryboard(FrameworkElement element, int duration)
{
var sb = new Storyboard();
var scale = new ScaleTransform(1, 1);
element.RenderTransform = scale;
element.RegisterName("scale", scale);
var animation = new DoubleAnimation
{
From = 1,
To = 0,
Duration = TimeSpan.FromMilliseconds(duration),
FillBehavior = FillBehavior.Stop,
EasingFunction = new QuinticEase { EasingMode = EasingMode.EaseIn }
};
sb.Children.Add(animation);
Storyboard.SetTarget(animation, scale);
Storyboard.SetTargetProperty(animation, new PropertyPath(ScaleTransform.ScaleXProperty));
return sb;
}
我知道我只为 X 轴设置了动画——只是想先让一些东西工作。