Esempio n. 1
0
        protected override void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                if (_formsCell != null)
                {
                    _formsCell.PropertyChanged -= CellPropertyChanged;
                }

                CustomCell = null;

                _heightConstraint?.Dispose();
                _heightConstraint = null;

                IVisualElementRenderer renderer = null;
                if (_rendererRef != null && _rendererRef.TryGetTarget(out renderer) && renderer.Element != null)
                {
                    FormsInternals.DisposeModelAndChildrenRenderers(renderer.Element);
                    _rendererRef = null;
                }

                renderer?.Dispose();

                _formsCell = null;
            }

            _disposed = true;

            base.Dispose(disposing);
        }
Esempio n. 2
0
        protected virtual void UpdateCell(View cell)
        {
            if (_formsCell != null)
            {
                _formsCell.PropertyChanged -= CellPropertyChanged;
            }
            _formsCell = cell;
            _formsCell.PropertyChanged += CellPropertyChanged;

            IVisualElementRenderer renderer;

            if (_rendererRef == null || !_rendererRef.TryGetTarget(out renderer))
            {
                renderer = GetNewRenderer();
            }
            else
            {
                if (renderer.Element != null && renderer == Platform.GetRenderer(renderer.Element))
                {
                    renderer.Element.ClearValue(FormsInternals.RendererProperty);
                }

                var type            = Xamarin.Forms.Internals.Registrar.Registered.GetHandlerTypeForObject(this._formsCell);
                var reflectableType = renderer as System.Reflection.IReflectableType;
                var rendererType    = reflectableType != null?reflectableType.GetTypeInfo().AsType() : renderer.GetType();

                if (rendererType == type || (renderer.GetType() == FormsInternals.DefaultRenderer) && type == null)
                {
                    renderer.SetElement(this._formsCell);
                }
                else
                {
                    //when cells are getting reused the element could be already set to another cell
                    //so we should dispose based on the renderer and not the renderer.Element
                    FormsInternals.DisposeRendererAndChildren(renderer);
                    renderer = GetNewRenderer();
                }
            }

            Platform.SetRenderer(this._formsCell, renderer);
            UpdateNativeCell();
            SetNeedsLayout();
        }
Esempio n. 3
0
        public virtual void UpdateCell(View cell, UITableView tableView)
        {
            if (_formsCell == cell && !CustomCell.IsForceLayout)
            {
                return;
            }
            CustomCell.IsForceLayout = false;

            if (_formsCell != null)
            {
                _formsCell.PropertyChanged -= CellPropertyChanged;
            }
            _formsCell = cell;
            _formsCell.PropertyChanged += CellPropertyChanged;

            IVisualElementRenderer renderer;

            if (_rendererRef == null || !_rendererRef.TryGetTarget(out renderer))
            {
                renderer = GetNewRenderer();
            }
            else
            {
                if (renderer.Element != null && renderer == Platform.GetRenderer(renderer.Element))
                {
                    renderer.Element.ClearValue(FormsInternals.RendererProperty);
                }

                var type            = Xamarin.Forms.Internals.Registrar.Registered.GetHandlerTypeForObject(this._formsCell);
                var reflectableType = renderer as System.Reflection.IReflectableType;
                var rendererType    = reflectableType != null?reflectableType.GetTypeInfo().AsType() : renderer.GetType();

                if (rendererType == type || (renderer.GetType() == FormsInternals.DefaultRenderer) && type == null)
                {
                    renderer.SetElement(this._formsCell);
                }
                else
                {
                    //when cells are getting reused the element could be already set to another cell
                    //so we should dispose based on the renderer and not the renderer.Element
                    FormsInternals.DisposeRendererAndChildren(renderer);
                    renderer = GetNewRenderer();
                }
            }

            Platform.SetRenderer(this._formsCell, renderer);

            if (!CustomCell.IsMeasureOnce || tableView.Frame.Width != _lastFrameWidth)
            {
                _lastFrameWidth = tableView.Frame.Width;
                var height = double.PositiveInfinity;
                var width  = tableView.Frame.Width - (CustomCell.UseFullSize ? 0 : 32); // CellBaseView layout margin
                var result = renderer.Element.Measure(tableView.Frame.Width, height, MeasureFlags.IncludeMargins);
                _lastMeasureWidth = result.Request.Width;
                if (_formsCell.HorizontalOptions.Alignment == LayoutAlignment.Fill)
                {
                    _lastMeasureWidth = width;
                }
                _lastMeasureHeight = result.Request.Height;

                if (_heightConstraint != null)
                {
                    _heightConstraint.Active = false;
                    _heightConstraint?.Dispose();
                }

                _heightConstraint          = renderer.NativeView.HeightAnchor.ConstraintEqualTo((nfloat)_lastMeasureHeight);
                _heightConstraint.Priority = 999f;
                _heightConstraint.Active   = true;

                renderer.NativeView.UpdateConstraintsIfNeeded();
            }

            Layout.LayoutChildIntoBoundingRegion(_formsCell, new Rectangle(0, 0, _lastMeasureWidth, _lastMeasureHeight));

            UpdateNativeCell();
        }