Attempts to animate a group of lines

I am making a pattern like this ...

Hexagon Pattern

By sending the storyboard to a linear array and performing calculations. Although, I am having trouble running multi-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 didn't bother doing the math behind it because I can't run storyboards at the same time. Or should I put everything doubleAnimations

in one storyboard? Both of these approaches give me stackoverflow. I'm relatively new to WPF C # programming, so any advice or additional information would be extremely helpful.

Editing the message to enable the Exception:

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll

Additional information: An exception was thrown by the target of the call.

+3


source to share


1 answer


Your code is mostly fine. I needed to make minor changes to get it working. I created a new window called new_Window

to test and create an object Canvas

on it called canMain

. Then I just added one line of code to yours: inside your loop, for

I had to add line[i] = new Line();

because the line objects were empty 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 Canvas

to the window first because I wanted to make sure the surface was ready to render.

+2


source







All Articles