I am trying to support dark mode and light mode in my app. It works perfectly except with the EntryCell. Seems like it has its own background and I tried dynamically set the entrycell background but that did not work. Its not reacting with dynamic resource. and I am out of luck trying to dynamically set the background color for EntryCell. Right now I can only set to Black or White background. Any ideas? Here's my:
This is my page renderer
[assembly: ExportRenderer(typeof(ContentPage), typeof(Mobile.Base.iOS.PageRenderer))]
namespace Mobile.Base.iOS
{
public class PageRenderer : Xamarin.Forms.Platform.iOS.PageRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
try
{
SetAppTheme();
}
catch (Exception ex)
{
Console.WriteLine($"\t\t\tERROR: {ex.Message}");
}
}
public override void TraitCollectionDidChange(UITraitCollection previousTraitCollection)
{
base.TraitCollectionDidChange(previousTraitCollection);
Console.WriteLine($"TraitCollectionDidChange: {TraitCollection.UserInterfaceStyle} != {previousTraitCollection.UserInterfaceStyle}");
if (previousTraitCollection != null && TraitCollection.UserInterfaceStyle != previousTraitCollection.UserInterfaceStyle)
{
SetAppTheme();
}
}
private void SetAppTheme()
{
if (TraitCollection.UserInterfaceStyle == UIUserInterfaceStyle.Dark)
{
if (App.AppTheme == "dark")
return;
//Add a Check for App Theme since this is called even when not changed really
App.Current.Resources = new DarkTheme();
App.AppTheme = "dark";
}
else
{
if (App.AppTheme != "dark")
return;
App.Current.Resources = new LightTheme();
App.AppTheme = "light";
}
}
}
}
This is my custom entry cell renderer
namespace Mobile.Base.iOS
{
public class customEntryCell : EntryCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var nativeCell = (EntryCell)item;
var cell = base.GetCell(nativeCell, reusableCell, tv);
((UITextField)cell.Subviews[0].Subviews[0]).BackgroundColor = UIColor.White;
return cell;
}
}
}