Animation adding is generally in XAML, but at some point in time it is necessary to dynamically use C # To write the animation logic.
Use storyboard. Begin
The first method is relatively simple. For example, a rectangle has been defined in XAML:
<Rectangle name = "rectangle"/>
We change the rectangle color from red to green:
VaR storyboard = new storyboard ();
Storyboard. settarget (storyboard, rectangle );
Storyboard. settargetproperty (storyboard, new propertypath ("fill. Color "));
VaR coloranm = new coloranimation (colors. Red, new duration (timespan. fromseconds (1 )));
Coloranm. From = colors. Green;
Storyboard. Children. Add (coloranm );
Storyboard. Begin ();
Note that the settargetproperty of storyboard requires a propertypath object. Here, the propertypath is used to specify the color attribute of the fill attribute.
Use animatable. beginanimation
If you use animatable. beginanimation directly, you can only use the solidinanimation method of solidcolorbrush. Because the control freezable. isfrozen directly defined on XAML is true. Therefore, we can only manually create the object to be animated, set our own solidcolorbrush that is not frozen (freezable. isfrozen = false), and use the solidinanimation of this solidcolorbrush to start the animation.
Code:
VaR solid = new solidcolorbrush (colors. Green );
// Create a rectangle
VaR rec = new rectangle () {fill = solid };
// Add a rectangle to the interface
Grid. Children. Clear ();
Grid. Children. Add (REC );
VaR coloranm = new coloranimation (colors. Red, new duration (timespan. fromseconds (1 )));
Coloranm. From = colors. Green;
Solid. beginanimation (solidcolorbrush. colorproperty, coloranm );