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

.NET 8下Android端MAUI Picker白色下划线移除问题

.NET 8 MAUI Android Picker 白色下划线无法移除的解决方法

升级到.NET 8后,Android端MAUI Picker的白色下划线无法通过原.NET 7中的Handler方法移除,已尝试的两种方案(App.cs映射、自定义Handler)仅能移除padding,下划线仍存在。

问题原因

.NET 8中MAUI对Android端Picker的底层实现做了变更,默认启用了Material Design的**背景色调(BackgroundTint)和轮廓(Outline)**属性,原来直接修改PlatformView背景的方式会被这些新属性覆盖,导致下划线无法移除。

可行解决方案

方案1:修改App.cs中的映射代码

public App()
{
    InitializeComponent();
    MainPage = new AppShell();

    Microsoft.Maui.Handlers.PickerHandler.Mapper.AppendToMapping(nameof(Picker), (handler, view) =>
    {
#if ANDROID
        var platformView = handler.PlatformView;
        // 清除原有背景设置
        platformView.Background = null;
        platformView.SetBackgroundColor(Android.Graphics.Color.Transparent);
        platformView.SetPadding(0, 0, 0, 0);

        // 关键:移除背景色调和轮廓
        platformView.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Android.Graphics.Color.Transparent);
        platformView.OutlineProvider = Android.Views.ViewOutlineProvider.None;

        // 可选:设置透明渐变背景兜底
        Android.Graphics.Drawables.GradientDrawable gd = new Android.Graphics.Drawables.GradientDrawable();
        gd.SetStroke(0, Android.Graphics.Color.Transparent);
        gd.SetColor(Android.Graphics.Color.Transparent);
        platformView.SetBackground(gd);
#endif
    });
}

方案2:修改自定义CleanPickerHandler

public class CleanPickerHandler : PickerHandler
{
#if ANDROID
    protected override void ConnectHandler(MauiPicker platformView)
    {
        base.ConnectHandler(platformView);

        platformView.Background = null;
        platformView.SetBackgroundColor(Android.Graphics.Color.Transparent);
        platformView.SetPadding(0, 0, 0, 0);

        // 新增关键代码:移除背景色调和轮廓
        platformView.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Android.Graphics.Color.Transparent);
        platformView.OutlineProvider = Android.Views.ViewOutlineProvider.None;

        Android.Graphics.Drawables.GradientDrawable gd = new Android.Graphics.Drawables.GradientDrawable();
        gd.SetStroke(0, Android.Graphics.Color.Transparent);
        gd.SetColor(Android.Graphics.Color.Transparent);
        platformView.SetBackground(gd);
    }
#endif
}

内容的提问来源于stack exchange,提问作者Christopher Townsend

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 12:35:56