I'm creating a composition-based animation which I would like to scale-in each ListViewItem as they are loaded. I have the following sample code:
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;
}
}
My issue is with the timing of the events. When clicking the button, the news set of items are immediately loaded and after a very quick pause, the animations kick in. Is there any other events or something I can change so that the items are not visible until the animations start??
Here is a link to the source code for the sample.
It looks like the listview is trying to play the EntranceThemeTransition before playing the feathering-in animation. If you're building your own animations, you could try removing the existing ListView.ItemContainerTransitions collection.