MAUI自定义Entry/Editor在Android11/13下文本及占位符异常问题
一、Android平台特定版本UI异常
MAUI应用中自定义Entry和Editor的占位符颜色与文本颜色无法正常显示,UI呈现空白状态。该问题仅出现在Android 11和Android 13系统中,Android 10与Android 12可正常运行。
自定义Entry渲染器代码
public class CustomEntryRenderer : EntryRenderer { public CustomEntryRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) { base.OnElementChanged(e); if (Control != null) { GradientDrawable gd = new GradientDrawable(); gd.SetColor(global::Android.Graphics.Color.Transparent); this.Control.Background = gd; this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions); } } }
自定义Editor渲染器代码
public class CustomNoUnderLineEditorRenderer : EditorRenderer { public CustomNoUnderLineEditorRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<Editor> e) { base.OnElementChanged(e); if (Control != null) { GradientDrawable gd = new GradientDrawable(); gd.SetColor(global::Android.Graphics.Color.Transparent); this.Control.SetBackgroundDrawable(gd); this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions); Control.SetHintTextColor(ColorStateList.ValueOf(global::Android.Graphics.Color.Gray)); } } }
XAML布局代码
<Frame x:Name="titleentry_frame" Margin="0,5,0,5" Style="{StaticResource CreateRequest_Editor_FrameStyle}"> <renderer:CustomEntry x:Name="title_entry" Keyboard="Default" Placeholder="Enter title" Style="{StaticResource CreateRequest_EntryStyle}"/> </Frame> <Frame x:Name="requesteditor_frame" Margin="0,11,0,11" Style="{StaticResource CreateRequest_Editor_FrameStyle}"> <renderer:CustomNoUnderLineEditor x:Name="description_editor" AutoSize="TextChanges" Placeholder="Enter description" Style="{StaticResource CreateRequest_EditorStyle}"> </renderer:CustomNoUnderLineEditor> </Frame>
二、iOS平台:渲染器迁移至Handler的需求(2023-11-02更新)
以下为原iOS平台自定义Entry和Editor的渲染器代码,需要迁移至Handler实现,求示例代码:
iOS自定义Entry渲染器
public class CustomEntryRenderer : EntryRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) { base.OnElementChanged(e); if (Control != null) { Control.BorderStyle = UITextBorderStyle.None; } } }
iOS自定义Editor渲染器
public class CustomEditorRenderer : EditorRenderer { public CustomEditorRenderer() { UIKeyboard.Notifications.ObserveWillShow((sender, args) => { if (Element != null) { Element.Margin = new Thickness(0, 0, 0, args.FrameEnd.Height); } }); UIKeyboard.Notifications.ObserveWillHide((sender, args) => { if (Element != null) { Element.Margin = new Thickness(0); } }); } protected override void OnElementChanged(ElementChangedEventArgs<Editor> e) { base.OnElementChanged(e); if (Control != null) { Control.BorderStyle = UITextBorderStyle.None; Control.Layer.CornerRadius = 10; Control.TextColor = UIColor.Black; } } }
三、iOS迁移后报错及残留问题(2023-11-03更新)
添加iOS迁移代码后出现以下编译错误:
Error CS0103 The name 'UITextBorderStyle' does not exist in the current context EntryMAUIDemo (net7.0-ios) MauiProgram.cs 43 Active
Error CS0103 The name 'UIColor' does not exist in the current context EntryMAUIDemo (net7.0-ios) MauiProgram.cs 59 Active
Error CS1061 'MauiTextView' does not contain a definition for 'BorderStyle' and no accessible extension method 'BorderStyle' accepting a first argument of type 'MauiTextView' could be found (are you missing a using directive or an assembly reference?) EntryMAUIDemo (net7.0-ios) MauiProgram.cs 61 Active
Error CS0103 The name 'UITextBorderStyle' does not exist in the current context EntryMAUIDemo (net7.0-ios) MauiProgram.cs 61 Active
移除报错相关代码后运行,仍存在文本颜色问题:
- Entry文本颜色不显示,Entry占位符颜色异常
- Editor文本及占位符颜色均异常
截图说明
- 截图1:占位符显示正常
- 截图2:仅Editor文本颜色正常,Entry文本不可见
设备信息(Android问题复现设备)
- 设备名称:OPPO A9 2020
- Android版本:11
- 型号:CPH1937
已将ModifyEntryandEditor()移至MauiProgram.cs,有更新后的示例项目可供复现。
内容的提问来源于stack exchange,提问作者Matthew Pans

