Reputation: 4170
I'm making a pattern similar to this...
By kicking off a story board per a line array and performing the calculations. Although, I'm having trouble nailing down how to kick off multiple line animations at the same time.
Here's what i've tried so far:
Canvas canMain = new Canvas();
canMain.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
canMain.Margin = new Thickness(50, 0, 0, 0);
Line[] line = new Line[6];
Storyboard sb;
DoubleAnimation da, da1;
for (int i = 0; i < line.Count(); i++)
{
line[i].Stroke = Brushes.Red;
line[i].StrokeThickness = 1;
line[i].X1 = i+11;
line[i].Y1 = i+11;
canMain.Children.Add(line[i]);
sb = new Storyboard();
da = new DoubleAnimation(line[i].Y1, 30, new Duration(TimeSpan.FromSeconds(0.5)));
da1 = new DoubleAnimation(line[i].X1, 30, new Duration(TimeSpan.FromSeconds(0.5)));
Storyboard.SetTargetProperty(da, new PropertyPath("(Line.Y2)"));
Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X2)"));
sb.Children.Add(da);
sb.Children.Add(da1);
line[i].BeginStoryboard(sb);
}
As you can tell I haven't began to do the math behind it because I can't seem to trigger the storyboards all at the same time. Or should I be stashing all the doubleAnimations
into the same storyboard? Both of these approaches gets me a stackoverflow. I am relatively new to WPF C# programming, so any advice or further information would be extremely helpful.
Editing post to include Exceptions:
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll
Additional information: Exception has been thrown by the target of an invocation.
Upvotes: 3
Views: 123
Reputation: 16899
Your code is mostly fine. I only had to make a minor change to get it working. I created a new window called new_Window
to test in and created a Canvas
object on it called canMain
.
Then I just added one line of code to yours: Inside your for
loop I had to add line[i] = new Line();
because the line objects were null at that point.
public new_Window()
{
InitializeComponent();
AnimateThis();
}
private void AnimateThis()
{
canMain.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
canMain.Margin = new Thickness(50, 0, 0, 0);
Line[] line = new Line[6];
Storyboard sb;
DoubleAnimation da, da1;
for (int i = 0; i < line.Count(); i++)
{
line[i] = new Line();
line[i].Stroke = Brushes.Red;
line[i].StrokeThickness = 1;
line[i].X1 = i+11;
line[i].Y1 = i+11;
canMain.Children.Add(line[i]);
sb = new Storyboard();
da = new DoubleAnimation(line[i].Y1, 30, new Duration(TimeSpan.FromSeconds(0.5)));
da1 = new DoubleAnimation(line[i].X1, 30, new Duration(TimeSpan.FromSeconds(0.5)));
Storyboard.SetTargetProperty(da, new PropertyPath("(Line.Y2)"));
Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X2)"));
sb.Children.Add(da);
sb.Children.Add(da1);
line[i].BeginStoryboard(sb);
}
}
I added the Canvas
to the window first because I wanted to make sure that there was a surface ready to be rendered on.
Upvotes: 2