我正在尝试将基于 XAML 的故事板转换为背后的代码,以便我可以同时在许多对象上使用故事板的多个实例。我编写了这个工厂方法来为我创建动画,但是当我启动故事板时,我似乎没有设置目标。有人可以帮助我吗,我还打算自学一种编写代码隐藏故事板的有效方法。
public static Storyboard CreateSimpleTranslation(double x, double y, TimeSpan timespan, FrameworkElement target)
{
// original storyboard
//<Storyboard x:Name="MoveUp">
// <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)"
// Storyboard.TargetName="itemsControl">
// <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
// <EasingDoubleKeyFrame KeyTime="0:0:0.02" Value="-122">
// <EasingDoubleKeyFrame.EasingFunction>
// <CircleEase EasingMode="EaseIn"/>
// </EasingDoubleKeyFrame.EasingFunction>
// </EasingDoubleKeyFrame>
// </DoubleAnimationUsingKeyFrames>
//</Storyboard>
var newSB = new Storyboard();
var da1 = new DoubleAnimationUsingKeyFrames();
Storyboard.SetTargetProperty(da1, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateX)"));
var da2 = new DoubleAnimationUsingKeyFrames();
Storyboard.SetTargetProperty(da1, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateY)"));
var ed1 = new EasingDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0)),
Value = 0
};
var ed2 = new EasingDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(timespan),
Value = x,
EasingFunction = new CircleEase() {EasingMode = EasingMode.EaseIn}
};
var ed3 = new EasingDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0)),
Value = 0
};
var ed4 = new EasingDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(timespan),
Value = y,
EasingFunction = new CircleEase() {EasingMode = EasingMode.EaseIn}
};
da1.KeyFrames.Add(ed1);
da1.KeyFrames.Add(ed2);
da2.KeyFrames.Add(ed3);
da2.KeyFrames.Add(ed4);
newSB.Children.Add(da1);
newSB.Children.Add(da2);
Storyboard.SetTarget(da1, target);
Storyboard.SetTarget(da2, target);
return newSB;
}