internal void UpdateView(object view, DataTemplate viewTemplate, ref UIView uiView, ref VisualElement formsElement) { // Is view set on the ItemsView? if (view == null) { if (formsElement != null) { //Platform.GetRenderer(formsElement)?.DisposeRendererAndChildren(); } uiView?.Dispose(); uiView = null; formsElement = null; } else { // Create the native renderer for the view, and keep the actual Forms element (if any) // around for updating the layout later (uiView, formsElement) = TemplateHelpers.RealizeView(view, viewTemplate, ItemsView); } }
public void Bind(DataTemplate template, object bindingContext, ItemsView itemsView) { var oldElement = PlatformHandler?.VirtualView as View; // 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); 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.GetHandler(view, itemsView.FindMauiContext()); 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 == Maui.Platform.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; }
static INativeViewHandler CreateHandler(View view, ItemsView itemsView) => TemplateHelpers.GetHandler(view, itemsView.FindMauiContext());