1
votes

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;
        }
    }
}

1
Seems like I found a work around on that, setting BackGroundColor to Transparent for TableView made it worked. But if you find other solution let me knowBigyan Devkota
How did you dynamically set the entrycell backgroundColor? You should change the backgroundColor in custom renderer.Jack Hua

1 Answers

0
votes

You can change the Entrycell's backgroundColor by changing cell.ContentView.BackgroundColor in custom renderer:

public class myEntryCelliOSCellRenderer : 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]).TextColor = UIColor.Orange;
        ((UITextField)cell.Subviews[0].Subviews[0]).BackgroundColor = UIColor.Green;

        //Change the entrycell backgroundColor
        cell.ContentView.BackgroundColor = UIColor.Red;

        return cell;
    }
}

Here is the source code of EntryCellRenderer you can check.