You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何去除.NET Maui中Entry控件获焦时的蓝色下划线

如何去除Entry控件获取焦点时出现的蓝色下划线?

问题场景

使用的Entry控件代码:

<Entry TextColor="Black" Keyboard="Numeric"  Placeholder="Personal Nummer" Text="{Binding EmailData.EmployeeID}"></Entry>

点击该Entry(获取焦点)时会显示蓝色下划线,需要彻底去除。

已尝试的无效方案

  • 在Entry上方添加灰色BoxView,下划线仍可见
  • 修改App.xaml中的<Color x:Key="MaterialEntryUnfocusedUnderlineColor">Transparent</Color>,无效果

当前全局Entry样式

<Style TargetType="Entry">
    <Setter Property="Visual" Value="Material"></Setter>
    <Setter Property="TextColor" Value="Black" />
    <Setter Property="BackgroundColor" Value="Transparent" />
    <Setter Property="PlaceholderColor" Value="#999999"></Setter>
    <Setter Property="FontFamily" Value="BrixSans"/>
    <Setter Property="FontSize" Value="14" />
    <Setter Property="PlaceholderColor" Value="{AppThemeBinding Light={StaticResource Gray200}, Dark={StaticResource Gray500}}" />
    <Setter Property="MinimumHeightRequest" Value="44"/>
    <Setter Property="MinimumWidthRequest" Value="44"/>
    <Setter Property="HorizontalOptions" Value="FillAndExpand"></Setter>
    <Setter Property="VisualStateManager.VisualStateGroups">
        <VisualStateGroupList>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal" />
                <VisualState x:Name="Disabled">
                    <VisualState.Setters>
                        <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Gray300}, Dark={StaticResource Gray600}}" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateGroupList>
    </Setter>
</Style>

有效解决方案

方案1:修改全局样式,添加聚焦状态的下划线设置

在全局Entry样式的CommonStates视觉状态组里,新增Focused状态,将下划线颜色设为透明:

<VisualStateGroup x:Name="CommonStates">
    <VisualState x:Name="Normal" />
    <VisualState x:Name="Disabled">
        <VisualState.Setters>
            <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Gray300}, Dark={StaticResource Gray600}}" />
        </VisualState.Setters>
    </VisualState>
    <!-- 新增聚焦状态设置 -->
    <VisualState x:Name="Focused">
        <VisualState.Setters>
            <Setter Property="UnderlineColor" Value="Transparent" />
        </VisualState.Setters>
    </VisualState>
</VisualStateGroup>

同时在App.xaml中添加聚焦状态的颜色资源(可选,配合Material视觉使用):

<Color x:Key="MaterialEntryFocusedUnderlineColor">Transparent</Color>

方案2:直接在单个Entry控件上设置下划线透明

无需修改全局样式,直接给目标Entry添加UnderlineColor属性:

<Entry TextColor="Black" Keyboard="Numeric"  Placeholder="Personal Nummer" 
       Text="{Binding EmailData.EmployeeID}" 
       UnderlineColor="Transparent"></Entry>

方案3:自定义Renderer(针对原生平台)

如果上述方法仍无效,可通过自定义渲染器彻底去除下划线:

Android端:

[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace YourAppNamespace.Droid
{
    public class CustomEntryRenderer : EntryRenderer
    {
        public CustomEntryRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                // 移除Entry的背景下划线
                Control.Background = null;
            }
        }
    }
}

iOS端:

[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace YourAppNamespace.iOS
{
    public class CustomEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                // 设置文本框无边框
                Control.BorderStyle = UITextBorderStyle.None;
            }
        }
    }
}

原因说明

之前修改的MaterialEntryUnfocusedUnderlineColor仅针对未聚焦状态的下划线,而问题出在聚焦状态的蓝色下划线,因此需要针对Focused状态或直接设置全局UnderlineColor来覆盖默认样式。

内容的提问来源于stack exchange,提问作者BLaZzeD_-_ leone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 22:33:16