简体   繁体   中英

How to read Storyboard property “From” or “To” (etc.)

I have created Storyboard by code and during animation I need to read some properties like From and To etc. How to do this? I need to get these information because there are some needs to add/remove/edit effects during animation.

Storyboard uses DoubleAnimation . Following short example shows how I get some values but I cannot find the way how to get From or To values. Timeline (or Storyboard ) do not have those values.

//GET EFFECT - THIS CALL GETS ADDED ANIMATION IN STORYBOARD 
Timeline TargetEffect = GetEffect(AnimationName, AnimationEffectType.Opacity);
//FROM
double From = (TargetElement as FrameworkElement).Opacity;
//DURATION
TimeSpan Duration = TargetEffect.Duration.TimeSpan;
//DELAY
TimeSpan Delay = TargetEffect.BeginTime.Value;
//REPEAT
RepeatBehavior Repeat = TargetEffect.RepeatBehavior;
//REVERSE
bool AutoReverse = TargetEffect.AutoReverse;
//FROM OR TO ????

In your code the reason you cannot access From and To is that you are using the general Timeline type, which does not have these properties defined (as only the derived types then define From and To with the specific type they animate). So to make it work, you will need to cast it to DoubleAnimation , as long as you know this is the type actually used:

var opacityAnimation = (DoubleAnimation)timeline;

From and To are then normal dependency properties (see here and here ). This means accessing their value works exactly the same way as with any other control's property.

In code you just do:

var fromValue = opacityAnimation.From;
var toValue = opacityAnimation.To;

To access the animation simpler, you can add a x:Name attribute to the animation itself in XAML:

<DoubleAnimation x:Name="MyAnimation" ... />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM