我正在创建一个基于合成的动画,我想在加载每个 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;
}
}
我的问题是事件的时间安排。单击按钮时,会立即加载项目的新闻集,并在非常快速的暂停后启动动画。是否有任何其他事件或我可以更改的东西,以便在动画开始之前项目不可见?
这是示例源代码的链接。