Summary of some weird behaviors in the background code writing animation, background animation

Source: Internet
Author: User

Summary of some weird behaviors in the background code writing animation, background animation

Let's take a look at a piece of code. All the examples in this article are based on this piece of code.

A rectangle rec is placed in the layout container. This rectangle is an animation-controlled object, a double-type animation.

 

Previously, when I wrote an animation in the background, I first registered a name for the object and connected the object (Rectangle) and the animation (DoubleAnimation) to the Storyboard through Storyboard. SetTargetName ).

For example, add a TranslateTransform to the rectangle to make the horizontal displacement of the rectangle by controlling the X attribute.

In this way, the animation can be moved. note that the last Begin () method must pass a parameter, which can be window (this), Layout container Layout, or rec of the rectangle itself. in short, you must pass a parameter, or the animation will not be able to be moved.

As for why, don't know. Go to check MSDN, fruitless. https://msdn.microsoft.com/zh-cn/library/cc190590 (v = vs.95). aspx

However, another even more critical problem is that subsequent operations such as temporary, continued, and stopped animation are all ineffective. you cannot call the Remove () method to trigger the Completed event. I still don't know why.

 

To solve this problem, I have summarized several situations.

The final result is the use of Storyboard. the SetTarget () method replaces Storyboard. setTargetName (). This avoids registration of meaningless names for objects. The Begin () method does not pass parameters. this will trigger the Completed event when you call the Remove Method Remove.

1. Change the dependency attribute of the object value type, such as Width and Height, and change the registration name directly to the registration object. Nothing else has changed.

2. change the dependent attributes of the object reference type, such as transforming RenderTransform. The object must register a rectangle rec instead of a TranslateTransform. The attribute path should be written in the attribute path syntax, instead of the new PropertyPath (TranslateTransform. XProperty ). it can be understood that the rectangle does not have the TranslateTransform. XProperty property.

Property path syntax see: https://msdn.microsoft.com/zh-cn/library/cc645024 (v = vs.95). aspx

3. at the same time, I only changed the conversion into a conversion group, focusing on the attribute path syntax. This syntax is not mentioned in msdn. I tried it by mistake and tried it out. This syntax is very strict, there are multiple or fewer parentheses, and none of them can be changed.

After that, paste the code that can be copied.

Private Storyboard m_sb; private void Window_Loaded (object sender, RoutedEventArgs e) {m_sb = Test ();} private void Button_Click (object sender, RoutedEventArgs e) {m_sb.Remove ();} private Storyboard Test () {Rectangle rec = new Rectangle () {Width = 50, Height = 50}; rec. fill = new SolidColorBrush (Color. fromRgb (255, 0, 0); Layout. children. add (rec); DoubleAnimation da = new DoubleAnimation (); da. duration = new Duration (TimeSpan. fromSeconds (2); da. from = 200; da. to = 50; da. repeatBehavior = RepeatBehavior. forever; da. autoReverse = true; // The Registration Name method // It Can Be dynamic, but the Storyboard's Begin () needs to be passed a parameter. you can pass this or the parent level, but it cannot be tentatively set to other subsequent operations to Remove () does not trigger Completed TranslateTransform tTf = new TranslateTransform (); rec. renderTransform = tTf; // The name cannot start with a number and cannot contain "-" string name = "tTf" + Guid. newGuid (). toString (). replace ("-", ""); this. registerName (name, tTf); Storyboard. setTargetName (da, name); Storyboard. setTargetProperty (da, new PropertyPath (TranslateTransform. XProperty); Storyboard sb = new Storyboard (); sb. completed + = sb_Completed; sb. children. add (da); sb. begin (this); return sb; // 1. change the dependency attribute of the object Value Type //// valid // Storyboard. setTarget (da, rec); // register by name and then register by object // Storyboard. setTargetProperty (da, new PropertyPath (Rectangle. widthProperty); // Storyboard sb = new Storyboard (); // sb. completed + = sb_Completed; // sb. children. add (da); // sb. begin (); // return sb; // 2. change the dependency attribute of the object reference type // TranslateTransform tTf = new TranslateTransform (); // rec. renderTransform = tTf; // invalid // Storyboard. setTarget (da, tTf); // conversion // Storyboard. setTargetProperty (da, new PropertyPath (TranslateTransform. XProperty); // does not follow the attribute path syntax //// invalid // Storyboard. setTarget (da, rec); // rectangle // Storyboard. setTargetProperty (da, new PropertyPath (TranslateTransform. XProperty); // not based on the attribute path syntax //// valid // Storyboard. setTarget (da, rec); // rectangle // Storyboard. setTargetProperty (da, new PropertyPath ("(Rectangle. renderTransform ). (TranslateTransform. x) "); // By attribute path syntax /// Storyboard // Storyboard sb = new Storyboard (); // sb. completed + = sb_Completed; // sb. children. add (da); // sb. begin (); // return sb; // 3. conversion group, attribute path syntax // TranslateTransform tTf = new TranslateTransform (); // ScaleTransform sTf = new ScaleTransform (); // TransformGroup tfg = new TransformGroup (); // tfg. children. add (tTf); // tfg. children. add (sTf); // rec. renderTransform = tfg; // valid // Storyboard. setTarget (da, rec); // rectangle // Storyboard. setTargetProperty (da, new PropertyPath ("(Rectangle. renderTransform ). children [0]. (TranslateTransform. x) "); // By attribute path syntax /// Storyboard // Storyboard sb = new Storyboard (); // sb. completed + = sb_Completed; // sb. children. add (da); // sb. begin (); // return sb;} void sb_Completed (object sender, EventArgs e ){}View Code

 

The principle is still unclear. If a great God knows it, please advise.

 

If this article is helpful to you, click "recommendation"

 

We recommend a good Blend, Wpf, and sl Communication Group.

Group number: 152049269

Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.