如何删除动画后';s已完成
本文关键字:已完成 动画 何删除 删除 | 更新日期: 2023-09-27 17:58:33
我想从目标属性中删除动画,因为我无法在动画后设置属性。
所以,根据上面问题的答案,我必须在动画完成后删除它。但是如何做到?
这是我的代码:
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();
动画完成后,我可以在哪里删除它?
只需将您的用法更改为:
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();
我在"从单个属性中删除动画"部分找到了我的解决方案。
我在我的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);
}