0

我正在创建一个基于合成的动画,我想在加载每个 ListViewItem 时对其进行缩放。我有以下示例代码:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        MyList.ContainerContentChanging += (sender, args) =>
        {
            if (!args.InRecycleQueue)
            {
                args.ItemContainer.Loaded += (item, e) =>
                {
                    var lvi = item as ListViewItem;
                    var panel = FindDescendant<ItemsStackPanel>(MyList);
                    var visual = ElementCompositionPreview.GetElementVisual(lvi);
                    var index = MyList.IndexFromContainer(lvi);

                    if (index >= panel.FirstVisibleIndex && index <= panel.LastVisibleIndex)
                    {
                        var width = (float)lvi.RenderSize.Width;
                        var height = (float)lvi.RenderSize.Height;

                        visual.CenterPoint = new Vector3(width / 2, height / 2, 0f);

                        var zoom = visual.Compositor.CreateVector3KeyFrameAnimation();
                        zoom.Duration = TimeSpan.FromSeconds(2);
                        zoom.DelayTime = TimeSpan.FromMilliseconds(index * 100);
                        zoom.InsertKeyFrame(0.0f, new Vector3(0.75f, 0.75f, 0f));
                        zoom.InsertKeyFrame(1.0f, new Vector3(1.0f, 1.0f, 0f));

                        visual.StartAnimation("Scale", zoom);
                    }
                };
            }
        };
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var col = new List<int>();

        for (var x = 0; x < 200; x++)
        {
            var z = new Random();
            col.Add(z.Next(0, 1000));
        }

        MyList.ItemsSource = null;
        MyList.ItemsSource = col;
    }

    private T FindDescendant<T>(DependencyObject element) where T : DependencyObject
    {
        T retValue = null;
        var childrenCount = VisualTreeHelper.GetChildrenCount(element);

        for (var i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(element, i);
            var type = child as T;

            if (type != null)
            {
                retValue = type;
                break;
            }

            retValue = FindDescendant<T>(child);

            if (retValue != null)
            {
                break;
            }
        }

        return retValue;
    }
}

在此处输入图像描述

我的问题是事件的时间安排。单击按钮时,会立即加载项目的新闻集,并在非常快速的暂停后启动动画。是否有任何其他事件或我可以更改的东西,以便在动画开始之前项目不可见?

这是示例源代码的链接。

4

2 回答 2

2

看起来列表视图正在尝试在播放羽化动画之前播放 EntranceThemeTransition。如果您正在构建自己的动画,您可以尝试删除现有的 ListView.ItemContainerTransitions 集合。

于 2019-01-17T20:28:34.463 回答
0

感谢@user10930282,他是对的。解决此问题的方法是删除 ListView 的默认 ItemContainerTransitions:

<ListView x:Name="MyList">
    .
    .
    .
    <ListView.ItemContainerTransitions>
        <TransitionCollection>
            <!-- Empty collection to not have any transitions interrupt the composition Item animations -->
        </TransitionCollection>
    </ListView.ItemContainerTransitions>
    .
    .
    .
</ListView>
于 2019-01-19T02:23:05.537 回答