关于在UWP中处理Scoped Batch动画

问题描述 投票:0回答:1

我正在使用Windows 10 Composition API在C#中创建动画。更具体地说,我使用here显示的方法将动画拼凑在一起,它正在完成我需要的东西。

我的问题是,该技术提供了一个事件End(),它在批处理完成时触发。我正在使用它在不同的UI元素上链接多个动画。我是否也应该使用这种方法来清理前一组动画,因为我不再需要它们了?无论如何,它们都是使用局部变量制作的。

这是我的代码详细说明我的意思:

  private void GreetingTB_Loaded(object sender, RoutedEventArgs e)
    {
        var _compositor = new Compositor();

         _compositor = ElementCompositionPreview.GetElementVisual(GreetingTB).Compositor;
        var _visual = ElementCompositionPreview.GetElementVisual(GreetingTB);

        var _batch = _compositor.CreateScopedBatch(CompositionBatchTypes.Animation);

        var animation = _compositor.CreateScalarKeyFrameAnimation();
        animation.Duration = new TimeSpan(0, 0, 0, 2, 0);
        animation.InsertKeyFrame(0.0f, 0.0f);
        animation.InsertKeyFrame(1.0f, 1.0f);

        _batch.Completed += Batch_Completed;
        GreetingTB.Text = "Hello!";
        _visual.StartAnimation("Opacity", animation);
        _batch.End();
    }

    private void Batch_Completed(object sender, CompositionBatchCompletedEventArgs args)
    {
        args.Dispose();

     // Create new animation here
    }

我调用了args.Dispose()方法,以防万一。但我想知道是否有更好的方法。是否需要使用“发件人”对象?

c# windows xaml memory-management uwp
1个回答
1
投票

因为最好在使用它们后立即处理实现IDisposable的对象,所以应该在事件处理程序中配置_batch。最简单的方法是将其包装在using语句中:

using (var _batch = _compositor.CreateScopedBatch(CompositionBatchTypes.Animation))
{
    ...
    _batch.End();
}

一旦批处理关闭,它就不能再使用了,所以请确保你不要尝试对sender事件处理程序中的Completed参数做任何事情。

© www.soinside.com 2019 - 2024. All rights reserved.