public void ValidateNoSizeWhenEmptyDataTemplate() { ItemsRepeater repeater = null; RunOnUIThread.Execute(() => { var elementFactory = new RecyclingElementFactory(); elementFactory.RecyclePool = new RecyclePool(); elementFactory.Templates["Item"] = (DataTemplate)XamlReader.Load( @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' />"); repeater = new ItemsRepeater() { ItemsSource = Enumerable.Range(0, 10).Select(i => string.Format("Item #{0}", i)), ItemTemplate = elementFactory, // Default is StackLayout, so do not have to explicitly set. // Layout = new StackLayout(), }; repeater.UpdateLayout(); // Asserting render size is zero Verify.IsLessThan(repeater.RenderSize.Width, 0.0001); Verify.IsLessThan(repeater.RenderSize.Height, 0.0001); }); }
public void ValidateBindingAndTemplateSelection() { RunOnUIThread.Execute(() => { var staticTemplate = (DataTemplate)XamlReader.Load( @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <TextBlock Text='static' /> </DataTemplate>" ); var bindTemplate = (DataTemplate)XamlReader.Load( @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <TextBlock Text='{Binding}' /> </DataTemplate>" ); var elementFactory = new RecyclingElementFactory() { RecyclePool = new RecyclePool(), Templates = { { "static", staticTemplate }, { "bind", bindTemplate } } }; elementFactory.SelectTemplateKey += delegate(RecyclingElementFactory sender, SelectTemplateEventArgs args) { args.TemplateKey = ((int)args.DataContext % 2 == 0) ? "bind" : "static"; }; const int numItems = 10; ItemsRepeater repeater = null; Content = CreateAndInitializeRepeater ( itemsSource: Enumerable.Range(0, numItems), elementFactory: elementFactory, layout: new StackLayout(), repeater: ref repeater ); Content.UpdateLayout(); Verify.AreEqual(numItems, VisualTreeHelper.GetChildrenCount(repeater)); for (int i = 0; i < numItems; i++) { var element = (TextBlock)repeater.TryGetElement(i); if (i % 2 == 0) { // Text is bound to the data for even indicies Verify.AreEqual(i.ToString(), element.Text); Verify.AreEqual(i, element.DataContext); } else { // Text explicitly set on the element only for odd indicies Verify.AreEqual("static", element.Text); } } }); }
public VirtualizingUniformStackLayoutSamplePage() { this.InitializeComponent(); elementFactory = (RecyclingElementFactory)Resources[nameof(elementFactory)]; repeater.ItemTemplate = elementFactory; repeater.ItemsSource = data; bringIntoView.Click += BringIntoView_Click; insert.Click += Insert_Click; }
public FlatSample() { this.InitializeComponent(); selectionModel = (SelectionModel)Resources[nameof(selectionModel)]; elementFactory = (RecyclingElementFactory)Resources[nameof(elementFactory)]; repeater.ItemTemplate = elementFactory; repeater.ItemsSource = _data; selectionModel.Source = _data; repeater.ElementPrepared += Repeater_ElementPrepared; repeater.ElementIndexChanged += Repeater_ElementIndexChanged; selectionModel.PropertyChanged += SelectionModel_PropertyChanged; }
public VirtualizingStackLayoutSamplePage() { this.InitializeComponent(); elementFactory = (RecyclingElementFactory)Resources[nameof(elementFactory)]; repeater.ItemTemplate = elementFactory; var rnd = new Random(); var data = new ObservableCollection <Recipe>(Enumerable.Range(0, 300).Select(k => new Recipe { ImageUri = new Uri(string.Format("pack://application:,,,/Images/recipe{0}.png", k % 8 + 1)), Description = k + " - " + _lorem.Substring(0, rnd.Next(50, 350)) })); repeater.ItemsSource = data; bringIntoView.Click += BringIntoView_Click; }
private void OnSelectTemplateKey(RecyclingElementFactory sender, SelectTemplateEventArgs args) { args.TemplateKey = (((int)args.DataContext) % 2 == 0) ? "even" : "odd"; }
private void OnSelectTemplateKey(RecyclingElementFactory sender, SelectTemplateEventArgs args) { args.TemplateKey = args.DataContext is string? "RepeaterItemTemplate" : "RepeaterGroupTemplate"; }
public void ValidateRecycling() { RunOnUIThread.Execute(() => { var elementFactory = new RecyclingElementFactory() { RecyclePool = new RecyclePool(), }; elementFactory.Templates["even"] = (DataTemplate)XamlReader.Load( @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <TextBlock Text='even' /> </DataTemplate>" ); elementFactory.Templates["odd"] = (DataTemplate)XamlReader.Load( @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <TextBlock Text='odd' /> </DataTemplate>" ); elementFactory.SelectTemplateKey += delegate(RecyclingElementFactory sender, SelectTemplateEventArgs args) { args.TemplateKey = ((int)args.DataContext % 2 == 0) ? "even" : "odd"; }; const int numItems = 10; ItemsRepeater repeater = new ItemsRepeater() { ItemsSource = Enumerable.Range(0, numItems), ItemTemplate = elementFactory, }; var context = (ElementFactoryGetArgs)RepeaterTestHooks.CreateRepeaterElementFactoryGetArgs(); context.Parent = repeater; var clearContext = (ElementFactoryRecycleArgs)RepeaterTestHooks.CreateRepeaterElementFactoryRecycleArgs(); clearContext.Parent = repeater; // Element0 is of type even, a new one should be created context.Data = 0; var element0 = elementFactory.GetElement(context); Verify.IsNotNull(element0); Verify.AreEqual("even", (element0 as TextBlock).Text); clearContext.Element = element0; elementFactory.RecycleElement(clearContext); // Element1 is of type odd, a new one should be created context.Data = 1; var element1 = elementFactory.GetElement(context); Verify.IsNotNull(element1); Verify.AreNotSame(element0, element1); Verify.AreEqual("odd", (element1 as TextBlock).Text); clearContext.Element = element1; elementFactory.RecycleElement(clearContext); // Element0 should be recycled for element2 context.Data = 2; var element2 = elementFactory.GetElement(context); Verify.AreEqual("even", (element2 as TextBlock).Text); Verify.AreSame(element0, element2); // Element1 should be recycled for element3 context.Data = 3; var element3 = elementFactory.GetElement(context); Verify.AreEqual("odd", (element3 as TextBlock).Text); Verify.AreSame(element1, element3); }); }
private void OnSelectTemplateKey(RecyclingElementFactory sender, SelectTemplateEventArgs args) { args.TemplateKey = (int.Parse(args.DataContext.ToString()) % 2 == 0) ? "even" : "odd"; }
private void OnSelectTemplateKey(RecyclingElementFactory sender, SelectTemplateEventArgs args) { args.TemplateKey = args.DataContext is Category ? "Category" : "Item"; }
private void ElementFactory_SelectTemplateKey(RecyclingElementFactory sender, SelectTemplateEventArgs args) { args.TemplateKey = args.DataContext is IEnumerable ? "group" : "item"; }