【问题标题】:How to set a background color and text color to an entry cell in xamarin.forms iOS?如何为 xamarin.forms iOS 中的条目单元格设置背景颜色和文本颜色?
【发布时间】:2020-04-14 05:58:29
【问题描述】:

我是 Xamarin 表单的新手。似乎没有属性可以在表格视图中为 EntryCell 设置背景颜色或文本颜色。当 iOS 的主题为 darkMode 时,有没有办法自定义?

DarkMode 将文本颜色更改为与背景颜色相同的白色。所以文本现在是不可见的

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.ios custom-renderer


    【解决方案1】:

    要将background colortext color 设置为xamarin.forms iOS 中的EntryCell,您可以使用自定义渲染器:

    [assembly: ExportRenderer(typeof(MyEntryCell), typeof(myEntryCelliOSCellRenderer))]
    namespace App99.iOS
    {
        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;
                return cell;
            }
        }
    }
    

    并在 Xamarin.forms 项目中使用它:

    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            InitializeComponent();
    
            TableView tableView = new TableView
            {
                Intent = TableIntent.Form,
                Root = new TableRoot
                {
                    new TableSection
                    {
                        new MyEntryCell
                        {
                            Label = "EntryCell:",
                            Placeholder = "Type Text Here",                           
                        }
                    }
                }
            };
    
            this.Content = new StackLayout
            {
                Children =
                {
                    tableView
                }
            };
        }
    }
    
    public class MyEntryCell : EntryCell { 
    
    }
    

    【讨论】:

    • Jack Hua,上面写着 GetCell - 没有合适的方法来覆盖,你要单独创建 GetCell 吗?
    • GetCell是EntryCellRenderer中的方法,你用的和我的代码一样吗?
    • @BigyanDevkota 我上传了一个示例here,您可以查看。如果对您有帮助,请记住接受此答案:)。
    • 你老板,它有效。我想我忘了使用命名空间,这就是我收到错误的原因。不过谢谢
    猜你喜欢
    • 2017-03-26
    • 1970-01-01
    • 2015-01-01
    • 2010-11-21
    • 2019-07-27
    • 2016-12-18
    • 1970-01-01
    • 2012-12-21
    • 2017-12-08
    相关资源
    最近更新 更多