Example #1
0
        internal void UpdateSubview(object view, DataTemplate viewTemplate, nint viewTag, ref UIView uiView, ref VisualElement formsElement)
        {
            uiView?.RemoveFromSuperview();

            if (formsElement != null)
            {
                ItemsView.RemoveLogicalChild(formsElement);
                formsElement.MeasureInvalidated -= OnFormsElementMeasureInvalidated;
            }

            UpdateView(view, viewTemplate, ref uiView, ref formsElement);

            if (uiView != null)
            {
                uiView.Tag = viewTag;
                CollectionView.AddSubview(uiView);
            }

            if (formsElement != null)
            {
                ItemsView.AddLogicalChild(formsElement);
            }

            if (formsElement != null)
            {
                RemeasureLayout(formsElement);
                formsElement.MeasureInvalidated += OnFormsElementMeasureInvalidated;
            }
            else if (uiView != null)
            {
                uiView.SizeToFit();
            }
        }
Example #2
0
        protected void UpdateSubview(object view, DataTemplate viewTemplate, ref UIView uiView, ref VisualElement formsElement)
        {
            if (uiView != null)
            {
                CollectionView.Subviews.Remove(uiView);
            }

            if (formsElement != null)
            {
                ItemsView.RemoveLogicalChild(formsElement);
                formsElement.MeasureInvalidated -= OnFormsElementMeasureInvalidated;
            }

            UpdateView(view, viewTemplate, ref uiView, ref formsElement);

            if (uiView != null)
            {
                CollectionView.AddSubview(uiView);
            }

            if (formsElement != null)
            {
                ItemsView.AddLogicalChild(formsElement);
            }

            if (formsElement != null)
            {
                RemeasureLayout(formsElement);
                formsElement.MeasureInvalidated += OnFormsElementMeasureInvalidated;
            }
            else if (uiView != null)
            {
                uiView.SizeToFit();
            }
        }
Example #3
0
        public void Bind(object itemBindingContext, ItemsView itemsView,
                         Action <Size> reportMeasure = null, Size?size = null)
        {
            var template = _template.SelectDataTemplate(itemBindingContext, itemsView);

            var templateChanging = template != _selectedTemplate;

            if (templateChanging)
            {
                // Clean up any content we're still holding on to
                _itemContentView.Recycle();

                // Create the new content
                View = (View)template.CreateContent();

                // Set the binding context _before_ we create the renderer; that way, the bound data will be
                // available during OnElementChanged
                View.BindingContext = itemBindingContext;

                // Actually create the native renderer
                _itemContentView.RealizeContent(View);
                _selectedTemplate = template;
            }

            _itemContentView.HandleItemSizingStrategy(reportMeasure, size);

            if (!templateChanging)
            {
                // Same template, new data
                View.BindingContext = itemBindingContext;
            }

            itemsView.AddLogicalChild(View);
        }
Example #4
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;
                    itemsView.RemoveLogicalChild(oldElement);
                    ClearSubviews();
                    _size = Size.Zero;
                }

                // Create the content and renderer for the view
                var view = itemTemplate.CreateContent() as 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
                    };
                }

                // 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;
        }
Example #5
0
        void ApplyTemplateAndDataContext(TemplatedCell cell, NSIndexPath indexPath)
        {
            // We need to create a renderer, which means we need a template
            var view = _itemsView.ItemTemplate.CreateContent() as View;

            _itemsView.AddLogicalChild(view);
            var renderer = CreateRenderer(view);

            BindableObject.SetInheritedBindingContext(view, _itemsSource[indexPath.Row]);
            cell.SetRenderer(renderer);
        }
        public override void SetBinding(EvasObject native, int index)
        {
            if (_nativeFormsTable.TryGetValue(native, out View view))
            {
                ResetBindedView(view);
                view.BindingContext = this[index];
                _dataBindedViewTable[this[index]] = view;

                view.MeasureInvalidated += OnItemMeasureInvalidated;
                _itemsView.AddLogicalChild(view);
            }
        }
Example #7
0
        public void Bind(object itemBindingContext, ItemsView itemsView)
        {
            var template = _template.SelectDataTemplate(itemBindingContext, itemsView);

            View = (View)template.CreateContent();
            _itemContentView.RealizeContent(View);

            // Set the binding context before we add it as a child of the ItemsView; otherwise, it will
            // inherit the ItemsView's binding context
            View.BindingContext = itemBindingContext;

            itemsView.AddLogicalChild(View);
        }
Example #8
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;
                    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;
        }
Example #9
0
        protected virtual void UpdateEmptyViewVisibility()
        {
            bool isEmpty = (CollectionViewSource?.View?.Count ?? 0) == 0;

            if (isEmpty)
            {
                if (_formsEmptyView != null)
                {
                    if (_emptyViewDisplayed)
                    {
                        ItemsView.RemoveLogicalChild(_formsEmptyView);
                    }

                    if (ItemsView.EmptyViewTemplate == null)
                    {
                        ItemsView.AddLogicalChild(_formsEmptyView);
                    }
                }

                if (_emptyView != null && ListViewBase is IEmptyView emptyView)
                {
                    emptyView.EmptyViewVisibility = WVisibility.Visible;

                    if (ActualWidth >= 0 && ActualHeight >= 0)
                    {
                        _formsEmptyView?.Layout(new Rect(0, 0, ActualWidth, ActualHeight));
                    }
                }

                _emptyViewDisplayed = true;
            }
            else
            {
                if (_emptyViewDisplayed)
                {
                    if (_emptyView != null && ListViewBase is IEmptyView emptyView)
                    {
                        emptyView.EmptyViewVisibility = WVisibility.Collapsed;
                    }

                    ItemsView.RemoveLogicalChild(_formsEmptyView);
                }

                _emptyViewDisplayed = false;
            }
        }
Example #10
0
        void UpdateEmptyViewVisibility(bool isEmpty)
        {
            if (isEmpty && _emptyUIView != null)
            {
                var emptyView = CollectionView.ViewWithTag(EmptyTag);

                if (emptyView != null)
                {
                    emptyView.RemoveFromSuperview();
                    ItemsView.RemoveLogicalChild(_emptyViewFormsElement);
                }

                _emptyUIView.Tag = EmptyTag;
                CollectionView.AddSubview(_emptyUIView);
                LayoutEmptyView();

                if (_emptyViewFormsElement != null)
                {
                    if (ItemsView.EmptyViewTemplate == null)
                    {
                        ItemsView.AddLogicalChild(_emptyViewFormsElement);
                    }

                    // Now that the native empty view's frame is sized to the UICollectionView, we need to handle
                    // the Forms layout for its content
                    _emptyViewFormsElement.Layout(_emptyUIView.Frame.ToRectangle());
                }

                _emptyViewDisplayed = true;
            }
            else
            {
                // Is the empty view currently in the background? Swap back to the default.
                if (_emptyViewDisplayed)
                {
                    _emptyUIView.RemoveFromSuperview();
                    _emptyUIView.Dispose();
                    _emptyUIView = null;

                    ItemsView.RemoveLogicalChild(_emptyViewFormsElement);
                }

                _emptyViewDisplayed = false;
            }
        }
Example #11
0
		void ShowEmptyView()
		{
			if (_emptyViewDisplayed || _emptyUIView == null)
			{
				return;
			}

			_emptyUIView.Tag = EmptyTag;
			CollectionView.AddSubview(_emptyUIView);

			if (((IElementController)ItemsView).LogicalChildren.IndexOf(_emptyViewFormsElement) == -1)
			{
				ItemsView.AddLogicalChild(_emptyViewFormsElement);
			}

			LayoutEmptyView();

			AlignEmptyView();
			_emptyViewDisplayed = true;
		}
        void ApplyTemplateAndDataContext(TemplatedCell cell, NSIndexPath indexPath)
        {
            var template = _itemsView.ItemTemplate;
            var item     = _itemsSource[indexPath.Row];

            // Run this through the extension method in case it's really a DataTemplateSelector
            template = template.SelectDataTemplate(item, _itemsView);

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

            cell.SetRenderer(renderer);

            // Bind the view to the data item
            view.BindingContext = _itemsSource[indexPath.Row];

            // And make sure it's a "child" of the ItemsView
            _itemsView.AddLogicalChild(view);
        }
Example #13
0
        void ShowEmptyView()
        {
            if (_emptyViewDisplayed || _emptyUIView == null)
            {
                return;
            }

            _emptyUIView.Tag = EmptyTag;
            CollectionView.AddSubview(_emptyUIView);

            if (!ItemsView.LogicalChildren.Contains(_emptyViewFormsElement))
            {
                ItemsView.AddLogicalChild(_emptyViewFormsElement);
            }

            LayoutEmptyView();

            AlignEmptyView();
            _emptyViewDisplayed = true;
        }
Example #14
0
        protected RecyclerView.ViewHolder CreateHeaderFooterViewHolder(object content, DataTemplate template, Context context)
        {
            if (template != null)
            {
                var footerContentView = new ItemContentView(context);
                return(new TemplatedItemViewHolder(footerContentView, template, isSelectionEnabled: false));
            }

            if (content is View formsView)
            {
                var viewHolder = SimpleViewHolder.FromFormsView(formsView, context);

                // Propagate the binding context, visual, etc. from the ItemsView to the header/footer
                ItemsView.AddLogicalChild(viewHolder.View);

                return(viewHolder);
            }

            // No template, Footer is not a Forms View, so just display Footer.ToString
            return(SimpleViewHolder.FromText(content?.ToString(), context, fill: false));
        }
Example #15
0
        public void Bind(object itemBindingContext, ItemsView itemsView,
                         Action <Size> reportMeasure = null, Size?size = null)
        {
            var template = _template.SelectDataTemplate(itemBindingContext, itemsView);

            if (template != _selectedTemplate)
            {
                _itemContentView.Recycle();
                View = (View)template.CreateContent();
                _itemContentView.RealizeContent(View);
                _selectedTemplate = template;
            }

            _itemContentView.HandleItemSizingStrategy(reportMeasure, size);

            // Set the binding context before we add it as a child of the ItemsView; otherwise, it will
            // inherit the ItemsView's binding context
            View.BindingContext = itemBindingContext;

            itemsView.AddLogicalChild(View);
        }
        void UpdateEmptyViewVisibility(bool isEmpty)
        {
            if (isEmpty && _emptyUIView != null)
            {
                if (!_currentBackgroundIsEmptyView)
                {
                    // Cache any existing background view so we can restore it later
                    _backgroundUIView = CollectionView.BackgroundView;
                }

                // Replace any current background with the EmptyView. This will also set the native empty view's frame
                // to match the UICollectionView's frame
                CollectionView.BackgroundView = _emptyUIView;

                if (_emptyViewFormsElement != null)
                {
                    if (ItemsView.EmptyViewTemplate == null)
                    {
                        ItemsView.AddLogicalChild(_emptyViewFormsElement);
                    }

                    // Now that the native empty view's frame is sized to the UICollectionView, we need to handle
                    // the Forms layout for its content
                    _emptyViewFormsElement.Layout(_emptyUIView.Frame.ToRectangle());
                }

                _currentBackgroundIsEmptyView = true;
            }
            else
            {
                // Is the empty view currently in the background? Swap back to the default.
                if (_currentBackgroundIsEmptyView)
                {
                    CollectionView.BackgroundView = _backgroundUIView;
                    ItemsView.RemoveLogicalChild(_emptyViewFormsElement);
                }

                _currentBackgroundIsEmptyView = false;
            }
        }
Example #17
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;
        }
        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            var context = parent.Context;

            // Does the ItemsView have a DataTemplate?
            var template = ItemsView.ItemTemplate;

            if (template == null)
            {
                // No template, just use the ToString view
                var view = new TextView(context);
                return(new TextViewHolder(view));
            }

            // Realize the content, create a renderer out of it, and use that
            var templateElement = (View)template.CreateContent();

            ItemsView.AddLogicalChild(templateElement);
            var itemContentControl = _createView(CreateRenderer(templateElement, context), context);

            return(new TemplatedItemViewHolder(itemContentControl, templateElement));
        }
Example #19
0
        internal void UpdateSubview(object view, DataTemplate viewTemplate, ref UIView uiView, ref VisualElement formsElement)
        {
            if (uiView != null)
            {
                CollectionView.Subviews.Remove(uiView);
            }

            if (formsElement != null)
            {
                ItemsView.RemoveLogicalChild(formsElement);
                formsElement.MeasureInvalidated -= OnFormsElementMeasureInvalidated;
            }

            UpdateView(view, viewTemplate, ref uiView, ref formsElement);

            if (uiView != null)
            {
                CollectionView.AddSubview(uiView);
            }

            if (formsElement != null)
            {
                ItemsView.AddLogicalChild(formsElement);
            }

            if (formsElement != null)
            {
                var request = formsElement.Measure(CollectionView.Frame.Width, double.PositiveInfinity, MeasureFlags.IncludeMargins);
                Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(formsElement, new Rectangle(0, -request.Request.Height, CollectionView.Frame.Width, request.Request.Height));

                formsElement.MeasureInvalidated += OnFormsElementMeasureInvalidated;
            }
            else if (uiView != null)
            {
                uiView.SizeToFit();
            }
        }