我想从我的目标媒体资源中移除动画,因为我的动画是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();
我在哪里可以删除动画它完成后?
也许一个愚蠢的问题,但如果你怎么设置动画? – lokusking
@lokusking我编辑了我的问题:) – Drarig29