删除故事板,但保持动画的价值? [英] Remove storyboard but keep animated value?

查看:218
本文介绍了删除故事板,但保持动画的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能删除故事板在XAML(在DataTrigger即RemoveStoryboard动作),但保持这是动画的值。类似于 Animatable.BeginAnimation


  

如果动画的的BeginTime为空,任何当前的动画将被删除,属性的当前值将举行。



解决方案

RemoveStoryboard的主要用途是去除动画值,并设定他们回到自己的非动画状态。在大多数情况下,你可以切换调用PauseStoryboard或StopStoryboard而是取决于具体情况。当你需要释放的故事板占用的资源,或将其用于其他目的的唯一的例外是。

如果你真的要删除的故事板,并保持属性值,则必须在属性直接设置动画的值。这可以通过每个值设置为动画的值来实现,这样的事:

 无效CopyAnimatedValues​​ToLocalValues​​(DependencyObject的OBJ)
{
  //递归下降树
  的for(int i = 0; I< VisualTreeHelper.GetChildrenCount(OBJ);我++)
    CopyAnimatedValues​​ToLocalValues​​(VisualTreeHelper.GetChild(OBJ I));  VAR枚举= obj.GetLocalValueEnumerator();
  而(enumerator.MoveNext())
  {
    VAR道具= enumerator.Current.Property;
    VAR值= enumerator.Current.Value作为可冻结;    //递归处理如。可由故事板被设置,只要它们不冻结刷子
    如果(值= NULL&放大器;!&安培;!value.IsFrozen)
      CopyAnimatedValues​​ToLocalValues​​(值);    // ***这是code的键位***
    如果(DependencyPropertyHelper.GetValueSource(OBJ,道具).IsAnimated)
      obj.SetValue(道具,obj.GetValue(丙));  }
}

您删除故事板复制的动画值之前,调用此权利。

修改:一种意见认为,这code可能是不必要的,因为调用BeginAnimation用的BeginTime = NULL达到类似的效果。

虽然这是事实,BeginAnimation用的BeginTime = NULL使得它看起来好像值复制到本地,以RemoveStoryboard以后的调用将导致该值恢复。这是因为与BeginAnimation =的BeginTime空导致动画之前举行其新的动画的开始挂起值,但并没有影响到当地的值。

在code上面居然覆盖当地的价值观,所以所有的动画可以被删除,对象将仍然有它们的新值。所以,如果你真的想叫RemoveStoryboard,仍然保持你的价值观,你将需要code我上面写的或类似的东西吧。

How can I remove a storyboard in XAML (i.e. RemoveStoryboard action in a DataTrigger) but keep the value that was animated. Similar to the Animatable.BeginAnimation:

If the animation's BeginTime is null, any current animations will be removed and the current value of the property will be held.

解决方案

RemoveStoryboard's primary use is to remove the animated values and set them back to their un-animated state. In most cases you can just switch the call to PauseStoryboard or StopStoryboard instead, depending on the specific case. The only exception is when you need to free up resources held by the storyboard or use it for another purpose.

If you really want to remove the storyboard and keep the property values, you must set the animated values directly on the properties. This can be done by setting each value to the animated value, something like this:

void CopyAnimatedValuesToLocalValues(DependencyObject obj)
{
  // Recurse down tree
  for(int i=0; i<VisualTreeHelper.GetChildrenCount(obj); i++)
    CopyAnimatedValuesToLocalValues(VisualTreeHelper.GetChild(obj, i));

  var enumerator = obj.GetLocalValueEnumerator();
  while(enumerator.MoveNext())
  {
    var prop = enumerator.Current.Property;
    var value = enumerator.Current.Value as Freezable;

    // Recurse into eg. brushes that may be set by storyboard, as long as they aren't frozen
    if(value!=null && !value.IsFrozen)
      CopyAnimatedValuesToLocalValues(value);

    // *** This is the key bit of code ***
    if(DependencyPropertyHelper.GetValueSource(obj, prop).IsAnimated)
      obj.SetValue(prop, obj.GetValue(prop));

  }
}

Call this right before you remove the storyboard to copy the animated values.

Edit A comment was made that this code may be unnecessary because calling BeginAnimation with BeginTime=null achieves a similar effect.

While it is true that BeginAnimation with BeginTime=null makes it looks as if the values were copied to local, a later call to RemoveStoryboard will cause the values to revert. This is because BeginAnimation with BeginTime=null causes the prior animation to hold its values pending the start of the new animation, but does nothing to affect the local values.

The code above actually overwrites local values, so all animations can be removed and the objects will still have their new values. So if you really want to call RemoveStoryboard and still keep your values, you will need the code I wrote above or something like it.

这篇关于删除故事板,但保持动画的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆