コード例 #1
0
ファイル: TemplatedCell.cs プロジェクト: terrajobst/maui
        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;
                    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.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);
            }
            else
            {
                // Same template, different data
                var currentElement = VisualElementRenderer?.Element;

                if (currentElement != null)
                {
                    currentElement.BindingContext = bindingContext;
                }
            }

            _currentTemplate = itemTemplate;
        }
コード例 #2
0
 internal void UpdateView(object view, DataTemplate viewTemplate, ref UIView uiView, ref VisualElement formsElement)
 {
     // Is view set on the ItemsView?
     if (view == null)
     {
         // Clear the cached Forms and native views
         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
         var(NativeView, FormsElement) = TemplateHelpers.RealizeView(view, viewTemplate, ItemsView);
         uiView       = NativeView;
         formsElement = FormsElement;
     }
 }
コード例 #3
0
        public void Bind(DataTemplate template, object bindingContext, ItemsView itemsView)
        {
            var oldElement = VisualElementRenderer?.Element;

            if (template != _currentTemplate)
            {
                // Remove the old view, if it exists
                if (oldElement != null)
                {
                    oldElement.MeasureInvalidated -= MeasureInvalidated;
                    itemsView.RemoveLogicalChild(oldElement);
                    ClearSubviews();
                    _size = Size.Zero;
                }

                // Create the content and renderer for the view
                var view     = template.CreateContent() as View;
                var renderer = TemplateHelpers.CreateRenderer(view);
                SetRenderer(renderer);
            }

            var currentElement = VisualElementRenderer?.Element;

            // Bind the view to the data item
            if (currentElement != null)
            {
                currentElement.BindingContext = bindingContext;
            }

            if (template != _currentTemplate)
            {
                // And make the 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(currentElement);
            }

            _currentTemplate = template;
        }
コード例 #4
0
ファイル: ItemsViewController.cs プロジェクト: mmitche/maui
        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);
            }
        }
コード例 #5
0
        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);
                    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;
        }