void SetRenderer(IVisualElementRenderer renderer) { VisualElementRenderer = renderer; var nativeView = VisualElementRenderer.NativeView; // Clear out any old views if this cell is being reused ContentView.ClearSubviews(); InitializeContentConstraints(nativeView); renderer.Element.MeasureInvalidated += MeasureInvalidated; }
public void Bind(DataTemplate template, object bindingContext, ItemsView itemsView) { var oldElement = VisualElementRenderer?.Element; // Run this through the extension method in case it's really a DataTemplateSelector var itemTemplate = template.SelectDataTemplate(bindingContext, itemsView); if (itemTemplate != CurrentTemplate) { // Remove the old view, if it exists if (oldElement != null) { oldElement.MeasureInvalidated -= MeasureInvalidated; oldElement.BindingContext = null; itemsView.RemoveLogicalChild(oldElement); ContentView.ClearSubviews(); _size = Size.Zero; } // Create the content and renderer for the view var view = itemTemplate.CreateContent() as View; // Set the binding context _before_ we create the renderer; that way, it's available during OnElementChanged view.BindingContext = bindingContext; var renderer = TemplateHelpers.CreateRenderer(view); SetRenderer(renderer); // And make the new Element a "child" of the ItemsView // We deliberately do this _after_ setting the binding context for the new element; // if we do it before, the element briefly inherits the ItemsView's bindingcontext and we // emit a bunch of needless binding errors itemsView.AddLogicalChild(view); // Prevents the use of default color when there are VisualStateManager with Selected state setting the background color // First we check whether the cell has the default selected background color; if it does, then we should check // to see if the cell content is the VSM to set a selected color if (SelectedBackgroundView.BackgroundColor == ColorExtensions.Gray && IsUsingVSMForSelectionColor(view)) { SelectedBackgroundView = new UIView { BackgroundColor = UIColor.Clear }; } } else { // Same template if (oldElement != null) { if (oldElement.BindingContext == null || !(oldElement.BindingContext.Equals(bindingContext))) { // If the data is different, update it // Unhook the MeasureInvalidated handler, otherwise it'll fire for every invalidation during the // BindingContext change oldElement.MeasureInvalidated -= MeasureInvalidated; oldElement.BindingContext = bindingContext; oldElement.MeasureInvalidated += MeasureInvalidated; UpdateCellSize(); } } } CurrentTemplate = itemTemplate; }