Reputation: 1274
I'm trying to convert a XAML based storyboard to a code behind one, so that I can use multiple instances of the storyboard on many objects simultaneously. I write this factory method to create the animation for me, but I seem to get Target not set, when I start the storyboard. can someone help me out, I also intend to teach myself an effective way of writing code-behind storyboards.
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;
}
Upvotes: 3
Views: 165
Reputation: 120
Try to recode it in following format it works right for me
public static Storyboard CreateSimpleTranslation(double x, double y, TimeSpan timespan, FrameworkElement target)
{
Storyboard storyboard = new Storyboard();
DoubleAnimationUsingKeyFrames da1 = new DoubleAnimationUsingKeyFrames();
var ed1 = new EasingDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0)),
Value = 0,
EasingFunction = new CircleEase() { EasingMode = EasingMode.EaseIn }
};
da1.KeyFrames.Add(ed1);
Storyboard.SetTarget(da1, target);
Storyboard.SetTargetProperty(da1, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateX)"));
storyboard.Children.Add(da1);
return storyboard;
}
Upvotes: 1