Ooui-tws-port/Ooui.Forms/Cells/CellRenderer.cs

68 lines
2.2 KiB
C#
Raw Normal View History

using Ooui.Forms.Extensions;
using System;
using Xamarin.Forms;
namespace Ooui.Forms.Cells
{
public class CellRenderer : IRegisterable
{
2018-04-26 23:55:23 +00:00
EventHandler _onForceUpdateSizeRequested;
static readonly BindableProperty RealCellProperty =
2018-04-26 23:55:23 +00:00
BindableProperty.CreateAttached ("RealCell", typeof (CellElement), typeof (Cell), null);
2018-04-26 23:55:23 +00:00
public virtual CellElement GetCellElement (Cell cell, CellElement reusableElement, List listView)
{
2018-04-26 23:55:23 +00:00
var cellElement = reusableElement ?? CreateCellElement (cell);
2018-04-26 23:55:23 +00:00
cellElement.Cell = cell;
2018-04-26 23:55:23 +00:00
WireUpForceUpdateSizeRequested (cell, cellElement);
UpdateBackground (cellElement, cell);
2018-04-26 23:55:23 +00:00
return cellElement;
}
2018-04-26 23:55:23 +00:00
protected static CellElement GetRealCell (BindableObject cell)
{
2018-04-26 23:55:23 +00:00
return (CellElement)cell.GetValue (RealCellProperty);
}
2018-04-26 23:55:23 +00:00
protected static void SetRealCell (BindableObject cell, CellElement renderer)
{
2018-04-26 23:55:23 +00:00
cell.SetValue (RealCellProperty, renderer);
}
2018-04-26 23:55:23 +00:00
protected virtual CellElement CreateCellElement (Cell cell)
{
2018-04-26 23:55:23 +00:00
return new CellElement ();
}
2018-04-26 23:55:23 +00:00
protected virtual void OnForceUpdateSizeRequest (Cell cell, CellElement cellElement)
{
2018-04-26 23:55:23 +00:00
cellElement.Style.Height = (int)cell.RenderHeight;
}
2018-04-26 23:55:23 +00:00
protected void UpdateBackground (CellElement cellElement, Cell cell)
{
var backgroundColor = Xamarin.Forms.Color.Default;
if (backgroundColor == Xamarin.Forms.Color.Default && cell.RealParent is VisualElement element)
backgroundColor = element.BackgroundColor;
2018-04-26 23:55:23 +00:00
cellElement.Style.BackgroundColor = backgroundColor.ToOouiColor (Xamarin.Forms.Color.White);
}
2018-04-26 23:55:23 +00:00
protected void WireUpForceUpdateSizeRequested (Cell cell, CellElement cellElement)
{
cell.ForceUpdateSizeRequested -= _onForceUpdateSizeRequested;
2018-04-26 23:55:23 +00:00
_onForceUpdateSizeRequested = (sender, e) => {
OnForceUpdateSizeRequest (cell, cellElement);
};
cell.ForceUpdateSizeRequested += _onForceUpdateSizeRequested;
}
}
}