Есть ли способ динамически установить фон для EntryCell в iOS в зависимости от Light Mode и DarkMode? - PullRequest
0 голосов
/ 07 января 2020

Я пытаюсь поддерживать темный режим и светлый режим в моем приложении. Он отлично работает, кроме как с EntryCell. Похоже, что у него есть собственный фон, и я попытался динамически установить фон для входной ячейки, но это не сработало. Не реагирует с ресурсом Dynami c. и мне не повезло, пытаясь динамически установить цвет фона для EntryCell. Прямо сейчас я могу установить только черный или белый фон. Любые идеи? Вот мой:

Это мой рендерер страниц


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

    }
}

Это мой пользовательский рендерер ячеек ввода


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 Ответ

0 голосов
/ 08 января 2020

Вы можете изменить Entrycell's backgroundColor, изменив cell.ContentView.BackgroundColor в пользовательском рендерере:

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

Вот исходный код EntryCellRenderer , который вы можете проверить.

...