Exemplo n.º 1
0
        internal UIContainerCell(string cellId, View view, Shell shell, object context) : base(UITableViewCellStyle.Default, cellId)
        {
            View = view;
            View.MeasureInvalidated += MeasureInvalidated;
            SelectionStyle           = UITableViewCellSelectionStyle.None;

            _renderer = Platform.GetRenderer(view);

            if (_renderer == null)
            {
                _renderer = Platform.CreateRenderer(view);
                Platform.SetRenderer(view, _renderer);
            }

            ContentView.AddSubview(_renderer.NativeView);
            _renderer.NativeView.ClipsToBounds = true;
            ContentView.ClipsToBounds          = true;

            BindingContext = context;
            if (shell != null)
            {
                shell.AddLogicalChild(View);
            }
        }
        public void SetCustomContent(View content)
        {
            if (content == Content)
            {
                return;
            }

            removeScolledEvent?.Invoke();
            removeScolledEvent = null;

            if (Content != null)
            {
                var oldRenderer    = Platform.GetRenderer(Content);
                var oldContentView = ContentView;
                var oldContent     = Content;

                Content     = null;
                ContentView = null;
                oldContent.ClearValue(Platform.RendererProperty);
                oldContentView?.RemoveFromSuperview();
                oldRenderer?.Dispose();
            }
            // If the user hasn't defined custom content then only the ContentView is set
            else if (ContentView != null)
            {
                var oldContentView = ContentView;
                ContentView = null;
                oldContentView.RemoveFromSuperview();
            }

            Content = content;
            if (Content != null)
            {
                var renderer = Platform.CreateRenderer(Content);
                ContentView = renderer.NativeView;
                Platform.SetRenderer(Content, renderer);
                ContentView.ClipsToBounds = true;

                // not sure if there's a more efficient way to do this
                // I can test the native control to see if it inherits from UIScrollView
                // But the CollectionViewRenderer doesn't inherit from UIScrollView
                if (Content is ScrollView sv)
                {
                    sv.Scrolled       += ScrollViewScrolled;
                    removeScolledEvent = () => sv.Scrolled -= ScrollViewScrolled;
                    void ScrollViewScrolled(object sender, ScrolledEventArgs e) =>
                    OnScrolled((nfloat)sv.ScrollY);
                }
                else if (Content is CollectionView cv)
                {
                    cv.Scrolled       += CollectionViewScrolled;
                    removeScolledEvent = () => cv.Scrolled -= CollectionViewScrolled;
                    void CollectionViewScrolled(object sender, ItemsViewScrolledEventArgs e) =>
                    OnScrolled((nfloat)e.VerticalOffset);
                }
                else if (Content is ListView lv)
                {
                    lv.Scrolled       += ListViewScrolled;
                    removeScolledEvent = () => lv.Scrolled -= ListViewScrolled;
                    void ListViewScrolled(object sender, ScrolledEventArgs e) =>
                    OnScrolled((nfloat)e.ScrollY);
                }
            }
        }