问题
[I created a DrawingLine
animation class to draw patterns.
The constructor starts the process of creating and animating lines:
internal DrawingLine(double x, double y, int _thickness, Brush _brush, Canvas _canvas)
At the end of the animation, this method generates a new line:
void CreateNewLine(object sender, EventArgs e)
{
Line newLine = new Line();
switch (lines.Count % 2 == 0)
{
case true:
{
if (lines.Count < 18)
{
newLine.X1 = 0;
newLine.Y1 = 180 - offset;
newLine.X2 = 0;
newLine.Y2 = 180 - offset;
}
// ... <-- Here is the math determines the positions of the points of the line
}
The method produces animation:
void AnimateXY()
{
DoubleAnimation lineXAnimation = new DoubleAnimation();
lineXAnimation.From = CurrentLine.X1;
lineXAnimation.To = to_X;
lineXAnimation.Duration = TimeSpan.FromSeconds(duration);
DoubleAnimation lineYAnimation = new DoubleAnimation();
lineYAnimation.From = CurrentLine.Y1;
lineYAnimation.To = to_Y;
lineYAnimation.Duration = TimeSpan.FromSeconds(duration);
lineYAnimation.Completed += CreateNewLine;
CurrentLine.BeginAnimation(Line.X2Property, lineXAnimation);
CurrentLine.BeginAnimation(Line.Y2Property, lineYAnimation);
}
Question: What is the best way to loop this animation, so that there are no memory leaks? I would also like to hear general recommendations for improving the structure of such classes. Picture of the pattern is attached.]1
回答1:
I found the answer to my question. If the class is full of animation, then it supports a recursive call (recursive creation). A recursive call makes the animation endless.
来源:https://stackoverflow.com/questions/61678709/wpf-pattern-animation-class