有没有一种方法可以根据Light Mode和DarkMode在iOS中动态设置EntryCell的背景?

问题描述 投票:0回答:1

我正在尝试在我的应用中支持暗模式和亮模式。除了EntryCell之外,它都能完美地工作。似乎它具有自己的背景,我尝试动态设置entrycell的背景,但这没有用。它不响应动态资源。我很幸运尝试动态设置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;
        }
    }
}

c# xamarin xamarin.forms xamarin.ios
1个回答
0
投票

您可以通过在自定义渲染器中更改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的源代码。

© www.soinside.com 2019 - 2024. All rights reserved.