The following code effectively creates and animates a single rectangle. What I would like to be able to do is create and animate multiple rectangles.
I get an error as soon as I change qtyX
and qtyY
to more than one.
Any idea how can I solve this issue?
Cannot register duplicate name 'MyAnimatedRectangleGeometry' in this scope.
XAML:
<Canvas Name="MyCanvas" Background="#FFFFF5F5"/>
Codebehind:
private void button_Click(object sender, RoutedEventArgs e)
{
double length = 100;
double height = 50;
int qtyX = 1;
int qtyY = 2;
for (int i = 0; i < qtyX; i++) {
for (int j = 0; j < qtyY; j++) {
RectangleGeometry myRectangleGeometry = new RectangleGeometry();
myRectangleGeometry.Rect = new Rect(((length + 1) * i), ((height + 1) * j), length, height);
this.RegisterName(
"MyAnimatedRectangleGeometry", myRectangleGeometry);
Path myPath = new Path();
myPath.Fill = Brushes.Blue;
myPath.Data = myRectangleGeometry;
RectAnimation myRectAnimation = new RectAnimation();
myRectAnimation.Duration = TimeSpan.FromSeconds(0.5);
myRectAnimation.FillBehavior = FillBehavior.HoldEnd;
myRectAnimation.From = new Rect(((length + 1) * i), ((height + 1) * j), length /2, height/2);
Storyboard.SetTargetName(myRectAnimation, "MyAnimatedRectangleGeometry");
Storyboard.SetTargetProperty(
myRectAnimation, new PropertyPath(RectangleGeometry.RectProperty));
Storyboard ellipseStoryboard = new Storyboard();
ellipseStoryboard.Children.Add(myRectAnimation);
myPath.Loaded += delegate (object s, RoutedEventArgs r) {
ellipseStoryboard.Begin(this);
};
MyCanvas.Children.Add(myPath);
}
}
}
FYI - I need to use RectangleGeometry.Rect
for performance purposes.