如果我在可视图层中创建并启动动画,我可以使用StopAnimation
来停止动画。但是,当我这样做时,似乎需要一段时间才能停止动画并使用最新值更新属性。那有什么办法吗?在下面的代码中,我等待10毫秒,但这有时只能起作用。
aws ecs list-container-instances --cluster default
答案 0 :(得分:0)
您可以使用CompositionScopedBatch
来解决此问题。需要注意的一点是,如果您有无限次的迭代次数,我会立即触发Completed
事件(我不知道为什么)。这是一个示例:
public sealed partial class MainPage : Page
{
SpriteVisual MyVisual;
CompositionScopedBatch ScopedBatch;
public MainPage()
{
this.InitializeComponent();
PointerReleased += MainPage_PointerReleased;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
MyVisual = compositor.CreateSpriteVisual();
MyVisual.Size = new Vector2(80, 80);
MyVisual.Offset = new Vector3(50, 50, 0);
MyVisual.Brush = compositor.CreateColorBrush(Colors.Green);
ElementCompositionPreview.SetElementChildVisual(this, MyVisual);
ScopedBatch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
var animation = compositor.CreateVector3KeyFrameAnimation();
animation.InsertKeyFrame(1, new Vector3(300, 50, 0));
animation.Duration = TimeSpan.FromSeconds(3);
// animation.IterationBehavior = AnimationIterationBehavior.Forever;
// CompositionScopedBatch.Completed is triggered immediately if Infinite is used.
// Use many iterations instead.
animation.IterationBehavior = AnimationIterationBehavior.Count;
animation.IterationCount = 1000000;
MyVisual.StartAnimation(nameof(MyVisual.Offset), animation);
ScopedBatch.Completed += CompositionScopedBatch_Completed;
ScopedBatch.End();
}
private void CompositionScopedBatch_Completed(object sender, CompositionBatchCompletedEventArgs args)
{
System.Diagnostics.Debug.WriteLine("Position when batch completed: " + MyVisual.Offset);
}
private void MainPage_PointerReleased(object sender, PointerRoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Position when animation running: " + MyVisual.Offset);
MyVisual.StopAnimation(nameof(MyVisual.Offset));
}
}