private RecyclingElementFactory GetElementFactory() { var elementFactory = new RecyclingElementFactory(); elementFactory.RecyclePool = new RecyclePool(); elementFactory.Templates["Item"] = SharedHelpers.GetDataTemplate(@"<TextBlock Text='{Binding}' Height='100' />"); return(elementFactory); }
private ItemsRepeater SetupRepeater(object dataSource, VirtualizingLayout layout, string itemContent, out ScrollViewer scrollViewer) { ItemsRepeater repeater = null; ScrollViewer sv = null; RunOnUIThread.Execute(() => { var elementFactory = new RecyclingElementFactory(); elementFactory.RecyclePool = new RecyclePool(); elementFactory.Templates["Item"] = (DataTemplate)XamlReader.Parse( @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> " + itemContent + @"</DataTemplate>"); repeater = new ItemsRepeater() { ItemsSource = dataSource, ItemTemplate = elementFactory, Layout = layout, HorizontalCacheLength = 0.0, VerticalCacheLength = 0.0 }; sv = new ScrollViewer { Content = repeater }; Content = new ItemsRepeaterScrollHost() { Width = 200, Height = 200, ScrollViewer = sv }; }); IdleSynchronizer.Wait(); scrollViewer = sv; return(repeater); }
public void ValidateElementAnimator() { ItemsRepeater repeater = null; ElementAnimatorDerived animator = null; var data = new ObservableCollection <string>(Enumerable.Range(0, 10).Select(i => string.Format("Item #{0}", i))); var renderingEvent = new ManualResetEvent(false); RunOnUIThread.Execute(() => { var elementFactory = new RecyclingElementFactory(); elementFactory.RecyclePool = new RecyclePool(); elementFactory.Templates["Item"] = (DataTemplate)XamlReader.Parse( @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <TextBlock Text='{Binding}' Height='50' /> </DataTemplate>"); CompositionTarget.Rendering += (sender, args) => { renderingEvent.Set(); }; repeater = new ItemsRepeater() { ItemsSource = data, ItemTemplate = elementFactory, }; Content = new ItemsRepeaterScrollHost() { Width = 400, Height = 800, ScrollViewer = new ScrollViewer { Content = repeater } }; }); IdleSynchronizer.Wait(); Verify.IsTrue(renderingEvent.WaitOne(), "Waiting for rendering event"); List <CallInfo> showCalls = new List <CallInfo>(); List <CallInfo> hideCalls = new List <CallInfo>(); List <CallInfo> boundsChangeCalls = new List <CallInfo>(); RunOnUIThread.Execute(() => { animator = new ElementAnimatorDerived() { HasShowAnimationValue = true, HasHideAnimationValue = true, HasBoundsChangeAnimationValue = true, StartShowAnimationFunc = (UIElement element, AnimationContext context) => { showCalls.Add(new CallInfo(repeater.GetElementIndex(element), context)); }, StartHideAnimationFunc = (UIElement element, AnimationContext context) => { hideCalls.Add(new CallInfo(repeater.GetElementIndex(element), context)); }, StartBoundsChangeAnimationFunc = (UIElement element, AnimationContext context, Rect oldBounds, Rect newBounds) => { boundsChangeCalls.Add(new CallInfo(repeater.GetElementIndex(element), context, oldBounds, newBounds)); } }; repeater.Animator = animator; renderingEvent.Reset(); data.Insert(0, "new item"); data.RemoveAt(2); }); Verify.IsTrue(renderingEvent.WaitOne(), "Waiting for rendering event"); IdleSynchronizer.Wait(); Verify.AreEqual(1, showCalls.Count); var call = showCalls[0]; Verify.AreEqual(0, call.Index); Verify.AreEqual(AnimationContext.CollectionChangeAdd, call.Context); Verify.AreEqual(1, hideCalls.Count); call = hideCalls[0]; Verify.AreEqual(-1, call.Index); // not in the repeater anymore Verify.AreEqual(AnimationContext.CollectionChangeRemove, call.Context); Verify.AreEqual(1, boundsChangeCalls.Count); call = boundsChangeCalls[0]; Verify.AreEqual(1, call.Index); Verify.AreEqual(AnimationContext.CollectionChangeAdd | AnimationContext.CollectionChangeRemove, call.Context); Verify.AreEqual(0, call.OldBounds.Y); Verify.AreEqual(50, call.NewBounds.Y); showCalls.Clear(); hideCalls.Clear(); boundsChangeCalls.Clear(); // Hookup just for show animations and validate. RunOnUIThread.Execute(() => { animator.HasShowAnimationValue = true; animator.HasHideAnimationValue = false; animator.HasBoundsChangeAnimationValue = false; renderingEvent.Reset(); data.Insert(0, "new item"); data.RemoveAt(2); }); Verify.IsTrue(renderingEvent.WaitOne(), "Waiting for rendering event"); IdleSynchronizer.Wait(); Verify.AreEqual(1, showCalls.Count); call = showCalls[0]; Verify.AreEqual(0, call.Index); Verify.AreEqual(AnimationContext.CollectionChangeAdd, call.Context); Verify.AreEqual(0, hideCalls.Count); Verify.AreEqual(0, boundsChangeCalls.Count); }