我有以下方法可行。我想把它放在一个返回Storyboard的实用程序方法中。我在将其转换为故事板时所做的每一次尝试都失败了,我花了很多时间研究。除非有人来救我,否则我已经准备好放弃了。
这是我要转换的代码:
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()。我知道这不应该那么难,但我只是没有得到它。
感谢。
编辑:在回应H.B的建议时,我尝试了以下代码,但仍无效:
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轴进行了动画制作 - 只是想先得到一些工作。
答案 0 :(得分:0)
您需要两个动画,然后使用SetTargetProperty
和SetTargetName
将附加的Storyboard
属性设置为在右侧对象上为右侧属性设置动画。
由于故事板的工作方式,您还需要设置名称范围(NameScope.SetNameScope
),注册变换名称,并使用StoryBoard.Begin
拨打containing element overload。
e.g。
NameScope.SetNameScope(element, new NameScope());
var transform = new ScaleTransform();
var transformName = "transform";
element.RegisterName(transformName, transform);
element.RenderTransform = transform;
var xAnimation = new DoubleAnimation(2, TimeSpan.FromSeconds(1));
var yAnimation = xAnimation.Clone();
var storyboard = new Storyboard()
{
Children = { xAnimation, yAnimation }
};
Storyboard.SetTargetProperty(xAnimation, new PropertyPath("(ScaleTransform.ScaleX)"));
Storyboard.SetTargetProperty(yAnimation, new PropertyPath("(ScaleTransform.ScaleY)"));
Storyboard.SetTargetName(xAnimation, transformName);
Storyboard.SetTargetName(yAnimation, transformName);
storyboard.Begin(element);
答案 1 :(得分:0)
我建议使用Expression Blend并从那里开始录制,它应该在XAML中创建你的故事板。而不是使用C#对其进行硬编码并尝试将其逐个1转换为故事板,因此它可能是一个容易出错的错误。