2016-07-25 165 views
1

我想從我的目標媒體資源中移除動畫,因爲我的動畫是Unable to set the property after its animation如何在動畫完成後刪除動畫

因此,根據上述問題的answer,我必須在動畫完成後刪除動畫。但是怎麼辦呢

這裏是我的代碼:

public class GridLengthAnimation : AnimationTimeline { 

    static GridLengthAnimation() { 
     FromProperty = DependencyProperty.Register("From", typeof(GridLength), typeof(GridLengthAnimation)); 
     ToProperty = DependencyProperty.Register("To", typeof(GridLength), typeof(GridLengthAnimation)); 
    } 

    public override Type TargetPropertyType { 
     get { return typeof(GridLength); } 
    } 

    protected override Freezable CreateInstanceCore() { 
     return new GridLengthAnimation(); 
    } 

    public ContentElement Target { get; set; } 

    public static readonly DependencyProperty FromProperty; 
    public GridLength From { 
     get { return (GridLength) GetValue(FromProperty); } 
     set { SetValue(FromProperty, value); } 
    } 

    public static readonly DependencyProperty ToProperty; 
    public GridLength To { 
     get { return (GridLength) GetValue(ToProperty); } 
     set { SetValue(ToProperty, value); } 
    } 

    public void BeginAnimation() { 
     Target.BeginAnimation(ColumnDefinition.WidthProperty, null); 
     Target.BeginAnimation(ColumnDefinition.WidthProperty, this); 
    } 

    public IEasingFunction EasingFunction { get; set; } = new CubicEase() { EasingMode = EasingMode.EaseInOut }; 

    public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock) { 
     double fromValue = ((GridLength) GetValue(FromProperty)).Value; 
     double toValue = ((GridLength) GetValue(ToProperty)).Value; 
     double newValue = 0; 

     if ((fromValue > toValue)) { 
      newValue = (1 - EasingFunction.Ease(animationClock.CurrentProgress.Value)) * (fromValue - toValue) + toValue; 
      return new GridLength(newValue, GridUnitType.Star); 
     } else { 
      newValue = EasingFunction.Ease(animationClock.CurrentProgress.Value) * (toValue - fromValue) + fromValue; 
      return new GridLength(newValue, GridUnitType.Star); 
     } 
    } 
} 

開始動畫,我做這種方式在我MainWindows.xaml.cs文件:

GridLengthAnimation gridAnim = new GridLengthAnimation() { 
    Name = "gridAnim", 
    Target = mainGrid.ColumnDefinitions[1], 
    Duration = new Duration(TimeSpan.FromMilliseconds(1000)) 
}; 

gridAnim.From = new GridLength(1, GridUnitType.Star); 
gridAnim.To = new GridLength(0, GridUnitType.Star); 

gridAnim.BeginAnimation(); 

我在哪裏可以刪除動畫它完成後?

+1

也許一個愚蠢的問題,但如果你怎麼設置動畫? – lokusking

+0

@lokusking我編輯了我的問題:) – Drarig29

回答

0

,我發現我的解決方案here,在部分「從個人財產刪除動畫」。

我在GridLengthAnimation添加了這個:

public void BeginAnimation() { 
    Completed += GridLengthAnimation_Completed; 
    Target.BeginAnimation(ColumnDefinition.WidthProperty, null); 
    Target.BeginAnimation(ColumnDefinition.WidthProperty, this); 
} 

private void GridLengthAnimation_Completed(object sender, EventArgs e) { 
    //unlocks the property 
    Target.BeginAnimation(ColumnDefinition.WidthProperty, null); 

    //holds the value at the end 
    ((ColumnDefinition) Target).Width = new GridLength(((GridLength) GetValue(ToProperty)).Value, GridUnitType.Star); 
} 
1

只要改變你的使用情況:

GridLengthAnimation gridAnim = new GridLengthAnimation() { 
      Name = "gridAnim", 
      Target = this.MainGrid.ColumnDefinitions[1], 
      Duration = new Duration(TimeSpan.FromMilliseconds(1000)) 
      }; 

      gridAnim.From = new GridLength(1, GridUnitType.Star); 
      gridAnim.To = new GridLength(0, GridUnitType.Star); 
//Cleanup on Complete 
      gridAnim.Completed += (sender, args) => { 
                var xx = sender as AnimationClock; 
                (xx.Timeline as GridLengthAnimation).Target = null; 
      }; 
      gridAnim.BeginAnimation(); 
+0

對不起,但它不工作...我認爲這是一個好主意,但屬性仍然鎖定。也許還有其他方法? – Drarig29