WPF/Silverlight deep solution: (1) Unlock associated attributes bound by storyboard

Source: Internet
Author: User

If you have used storyboard for animation development using WPF/Silverlight and modified the associated attributes, do you notice that these association attributes cannot be modified by directly assigning values? Although their values have been changed, they cannot be reflected on the interface. For exampleC #Develop WPF/SilverlightAnimation and game series tutorial (game tutorial)In this tutorial, storyboard is widely used to modify the X and Y coordinate Association attributes of a role and the role's direction orientation Association attributes in an animated manner; if you use storyboard for each change, the result is WYSIWYG. However, if you want to right-click the storyboard and change the orientation of the role to the North: Direction = 0 after you change the association attribute toward ction, the result will be no effect; many may suspect that the right-click event is incorrect? This is not the case. When you use storyboard in WPF/Silverlight to modify Association attributes, these association attributes are locked and cannot be modified by direct value assignment. Is it helpless? In fact, msdn has provided us with three solutions very early. The original address is:
Http://msdn.microsoft.com/zh-cn/library/aa970493.aspx
Below is my analysis and expansion of them:

Method 1: Set the fillbehavior attribute of the animation to stop:

Storyboard. fillbehavior = fillbehavior. Stop;

By setting fillbehavior to stop, the notification animation stops affecting its target association attributes after it reaches the end of its activity period. Although the purpose is achieved, it takes effect only after the animation ends. More importantly, all associated attribute values modified by storyboard are restored to the initial values. Therefore, this method is only suitable for making navigation menu buttons similar to those in a webpage: When the mouse is hovering over the menu, the menu graphic button performs a gorgeous change animation; when you move the mouse away, the image is changed back to the initial image.

Method 2: Removes the entire animation board ). This method must be similar

Leader. beginstoryboard (storyboard, handoffbehavior. snapshotandreplace, true); or

Storyboard. Begin (Leader, handoffbehavior. snapshotandreplace, true );

These two methods start the animation, and then pass

Storyboard. Remove (leader); storyboard = NULL; notifies the animation board animation to stop affecting the target association attribute of the leader object and remove the storyboard. Note that ① The iscontrollable parameter of the animation must be set to true; ② it is best to set handoffbehavior to snapshotandreplace. The function of this enumeration is: new animations replace any existing animations on the associated attributes they are applied.

Method 3: Removes an animation from a single association attribute. Take the wizard role orientation as an example. If this attribute has been locked by storyboard, you can change the value of this attribute by using the following method:

Leader. beginanimation (qxspirit. directionproperty, null); or

Leader. applyanimationclock (qxspirit. directionproperty, null );

These two methods prevent the animation associated with the leader from continuing to affect the direproperty Property Association attribute of the Leader (this method is also effective for non-animated board animation ). Then, we can easily assign values to the wizard's orientation attribute using leader. Direction = 0, which is displayed on the screen.

The above three solutions flexibly work with storyboard. Children. Clear () in WPF. They can be used to deal with almost any problems about storyboard locking Association attributes. However, they are often unsatisfactory in Silverlight. After all, it is only a subset of WPF and has little support for functions and methods. As a result, I have expanded the following two solutions. More importantly, they are all the ultimate Pharmaceutical Strategy common to WPF/Silverlight.

Method 4: You can pause the previous animation (Note: pause rather than stop) each time you run a new animation, for example, storyboard. pause (leader); then create a new animation board storyboard = new storyboard (); in this way, the target value of the Association Attribute Modified by storyboard will be the new storyboard as the starting attribute value, in this way, the Association attributes are perfectly connected between the animation and the animation. Some may ask if the previous storyboard will continue to occupy the memory space? For. we cannot control the memory recycle mechanism of. net. If you are not at ease, log out of the memory with storyboard = NULL before creating a new storyboard, in Silverlight animation, I did this. Practice has proved that this method is indeed achieved.

Method 5: Attack against viruses. Since the storyboard is locked, we can assign values to these associated attributes in the form of storyboard animation. The disadvantage of this method is that it has no performance. The advantage is that it is omnipotent: it is suitable for modifying all associated attributes locked by storyboard, whether in WPF or Silverlight. The following uses the wizard orientation as an example:
Doubleanimation = new doubleanimation ();

Doubleanimation. To = direction;

Doubleanimation. Duration = new duration (timespan. frommilliseconds (10 ));

Storyboard. settarget (doubleanimation, spirit );

Storyboard. settargetproperty (doubleanimation, new propertypath ("direction "));

Storyboard. Children. Add (doubleanimation );

Storyboard. Begin ();

In this way, the attributes associated with qxspirit. directionproperty are forcibly modified.

In this section, we hope that the above five solutions to unlock storyboard's associated attributes will be helpful for WPF/Silverlight animation development. At the same time, if you have written anything wrong or have any mistakes, please do not hesitate to inform us. I will promptly modify and correct them.

In the next section, I will explain how to protect the Silverlight source code.

Author: dark blue right hand
Source: http://alamiye010.cnblogs.com/
Download this series of directories and source code: Click to enter (welcome to join the WPF/Silverlight team WPF/Silverlight blog Team)
The copyright of this article is shared by the author and the blog Park. You are welcome to reprint it. However, this statement must be retained without the author's consent and the original article connection should be provided in a prominent position on the article page; otherwise, the right to pursue legal liability will be retained.

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.