我知道ViewExtensions.CancelAnimations(view);
。我已经试过了没用
我已经在Xamarin Forms Github repo上提出了这个问题,同时我希望有人可以提供一种解决方法。
我正在尝试在由ViewModel属性启动和停止的Label上创建比例动画。我已经创建了两个简单的TriggerAction<VisualElement>
来开始和停止
public class StartAnimationAction : TriggerAction<VisualElement>
{
public Animation Animation { set; get; }
protected override void Invoke(VisualElement view)
{
this.Animation.Commit(view, "ScaleIt", length: 30000, easing: Easing.Linear,
finished: (v, c) => view.Scale = 1, repeat: () => false);
}
}
public class StopAnimationAction : TriggerAction<VisualElement>
{
protected override void Invoke(VisualElement view)
{
ViewExtensions.CancelAnimations(view);
}
}
[StartAction上的动画属性是通过View设置的
var throbAnimation = new Animation(v => this.MyLabel.Scale = v, 1, 2);
[ViewModel通过DataTrigger完成
<Label x:Name="MyLabel" Text="Hello World">
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding IsSyncing}" Value="True">
<DataTrigger.EnterActions>
<settings:StartAnimationAction x:Name="StartAnimation"/>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<settings:StopAnimationAction/>
</DataTrigger.ExitActions>
</DataTrigger>
</Label.Triggers>
</Label>
ViewExtensions.CancelAnimations不会停止动画,并且继续调用回调和重复回调。
还有其他取消动画的方法,或者我是否错误地使用CancelAnimations?
当您通过调用Commit
方法获得动画时,可以通过调用AbortAnimation
扩展方法来取消动画。
更改
public class StopAnimationAction : TriggerAction<VisualElement>
{
protected override void Invoke(VisualElement view)
{
ViewExtensions.CancelAnimations(view);
}
}
到
public class StopAnimationAction : TriggerAction<VisualElement>
{
protected override void Invoke(VisualElement view)
{
view.AbortAnimation("ScaleIt");//the name you commit in startAnimationAciton
}
}