Move IElementConfiguration to an internal structure on Cell (#5850)
authorShane Neuville <shane94@hotmail.com>
Wed, 17 Apr 2019 17:59:16 +0000 (11:59 -0600)
committerGitHub <noreply@github.com>
Wed, 17 Apr 2019 17:59:16 +0000 (11:59 -0600)
* Move IElementConfiguration to an internal structure on Cell

* set custom ElementConfiguration to private

* fix comments

Xamarin.Forms.Core/Cells/Cell.cs
Xamarin.Forms.Platform.iOS/Cells/CellRenderer.cs

index 440773a..e5e4f20 100644 (file)
@@ -8,22 +8,22 @@ using Xamarin.Forms.Internals;
 
 namespace Xamarin.Forms
 {
-       public abstract class Cell : Element, ICellController, IElementConfiguration<Cell>, IFlowDirectionController, IPropertyPropagationController, IVisualController
+       // Don't add IElementConfiguration<Cell> because it kills performance on UWP structures that use Cells
+       public abstract class Cell : Element, ICellController, IFlowDirectionController, IPropertyPropagationController, IVisualController
        {
                public const int DefaultCellHeight = 40;
                public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create("IsEnabled", typeof(bool), typeof(Cell), true, propertyChanged: OnIsEnabledPropertyChanged);
 
                ObservableCollection<MenuItem> _contextActions;
+               readonly Lazy<ElementConfiguration> _elementConfiguration;
 
                double _height = -1;
 
                bool _nextCallToForceUpdateSizeQueued;
 
-               readonly Lazy<PlatformConfigurationRegistry<Cell>> _platformConfigurationRegistry;
-
                public Cell()
                {
-                       _platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<Cell>>(() => new PlatformConfigurationRegistry<Cell>(this));
+                       _elementConfiguration = new Lazy<ElementConfiguration>(() => new ElementConfiguration(this));
                }
 
                EffectiveFlowDirection _effectiveFlowDirection = default(EffectiveFlowDirection);
@@ -126,11 +126,6 @@ namespace Xamarin.Forms
                [EditorBrowsable(EditorBrowsableState.Never)]
                public event EventHandler ForceUpdateSizeRequested;
 
-               public IPlatformElementConfiguration<T, Cell> On<T>() where T : IConfigPlatform
-               {
-                       return _platformConfigurationRegistry.Value.On<T>();
-               }
-
                public void ForceUpdateSize()
                {
                        if (_nextCallToForceUpdateSizeQueued)
@@ -257,5 +252,39 @@ namespace Xamarin.Forms
                        if (e.PropertyName == "RowHeight")
                                OnPropertyChanging("RenderHeight");
                }
+
+
+               #region Nested IElementConfiguration<Cell> Implementation
+               // This creates a nested class to keep track of IElementConfiguration<Cell> because adding 
+               // IElementConfiguration<Cell> to the Cell itself tanks performance on UWP ListViews
+               // Issue has been logged with UWP
+               public IPlatformElementConfiguration<T, Cell> On<T>() where T : IConfigPlatform
+               {
+                       return GetElementConfiguration().On<T>();
+               }
+
+               IElementConfiguration<Cell> GetElementConfiguration()
+               {
+                       return _elementConfiguration.Value;
+               }
+
+               class ElementConfiguration : IElementConfiguration<Cell>
+               {
+                       readonly Lazy<PlatformConfigurationRegistry<Cell>> _platformConfigurationRegistry;
+                       public ElementConfiguration(Cell cell)
+                       {
+                               _platformConfigurationRegistry = 
+                                       new Lazy<PlatformConfigurationRegistry<Cell>>(() => new PlatformConfigurationRegistry<Cell>(cell));
+                       }
+
+                       public IPlatformElementConfiguration<T, Cell> On<T>() where T : IConfigPlatform
+                       {
+                               return _platformConfigurationRegistry.Value.On<T>();
+                       }
+
+                       internal PlatformConfigurationRegistry<Cell> Registry => _platformConfigurationRegistry.Value;
+               }
+               #endregion
+
        }
 }
\ No newline at end of file
index f2615bb..14f8ef4 100644 (file)
@@ -59,8 +59,11 @@ namespace Xamarin.Forms.Platform.iOS
                protected void UpdateBackground(UITableViewCell tableViewCell, Cell cell)
                {
                        var uiBgColor = UIColor.White; // Must be set to a solid color or blending issues will occur
-
-                       var  defaultBgColor = cell.OnThisPlatform().DefaultBackgroundColor();
+#if __MOBILE__
+                       var defaultBgColor = cell.On<PlatformConfiguration.iOS>().DefaultBackgroundColor();
+#else
+                       var defaultBgColor = cell.On<PlatformConfiguration.macOS>().DefaultBackgroundColor();
+#endif
                        if (defaultBgColor != Color.Default)
                        {
                                uiBgColor = defaultBgColor.ToUIColor();