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

.NET MAUI Picker与DatePicker如何禁止用户文本输入?

.NET MAUI 禁用 Picker、DatePicker 手动输入,仅允许选择预设选项

Picker 处理方案

部分平台(如 Android)的 Picker 默认带搜索输入框,要禁用手动输入、仅保留选择功能,可按以下两种方式处理:

方式一:自定义 Handler(跨平台适配更稳定)

在项目的 MauiProgram.cs 中注册自定义 Handler,修改 Android 原生 Spinner 的行为:

using Microsoft.Maui.Handlers;
using Android.Widget;

namespace 你的应用命名空间;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            });

#if ANDROID
        // 禁用 Picker 的手动输入
        PickerHandler.Mapper.AppendToMapping("DisableInput", (handler, view) =>
        {
            if (handler.PlatformView is AppCompatSpinner spinner)
            {
                spinner.SetInputType(Android.Text.InputTypes.Null);
                spinner.Clickable = true;
                spinner.Focusable = true;
            }
        });
#endif

        return builder.Build();
    }
}

方式二:XAML 直接设置属性(简单场景)

直接给 Picker 添加 IsReadOnly="True" 属性,部分平台会自动禁用输入:

<Picker Title="Selecciona un centro de trabajo"                          
        x:Name="CentroTrabajoPicker"
        HorizontalOptions="End" 
        BackgroundColor="Transparent"
        TextColor="Black"
        IsReadOnly="True"/>

DatePicker 处理方案

DatePicker 在部分平台允许手动输入日期,禁用输入的方式如下:

方式一:自定义 Handler

同样在 MauiProgram.cs 中添加 DatePicker 的 Handler 配置:

#if ANDROID
// 禁用 DatePicker 的手动输入
DatePickerHandler.Mapper.AppendToMapping("DisableInput", (handler, view) =>
{
    if (handler.PlatformView is Android.Widget.EditText editText)
    {
        editText.InputType = Android.Text.InputTypes.Null;
        editText.Focusable = false;
        editText.Clickable = true;
    }
});
#endif

方式二:XAML 设置属性

给 DatePicker 添加 IsReadOnly="True" 属性:

<DatePicker Date="{x:Static sys:DateTime.Now}" 
            x:Name="FechaPickerMenu"
            Format="dd/MM/yyyy"
            BackgroundColor="Transparent" 
            HorizontalOptions="End"
            IsReadOnly="True"/>

修改后的完整 XAML 代码

<StackLayout Grid.Row="0">
    <!-- Centro de trabajo y Picker -->
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <Label Grid.Column="0" Text="Centro de trabajo" VerticalOptions="Center"/>
        <Frame Grid.Column="2" BackgroundColor="{StaticResource Secondary}" Padding="15,0" CornerRadius="10" BorderColor="Black">
            <Picker Title="Selecciona un centro de trabajo"                          
                    x:Name="CentroTrabajoPicker"
                    HorizontalOptions="End" 
                    BackgroundColor="Transparent"
                    TextColor="Black"
                    IsReadOnly="True"/>
        </Frame>
    </Grid>

    <!-- Fecha y DatePicker -->
    <Grid Margin="0,20,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <Label Grid.Column="0" Text="Fecha" VerticalOptions="Center"/>
        <Frame Grid.Column="2" BackgroundColor="{StaticResource Secondary}"  Padding="15,0" CornerRadius="10" BorderColor="Black" >
            
            <DatePicker Date="{x:Static sys:DateTime.Now}" 
                        x:Name="FechaPickerMenu"
                        Format="dd/MM/yyyy"
                        BackgroundColor="Transparent" 
                        HorizontalOptions="End"
                        IsReadOnly="True"/>
        </Frame>
    </Grid>
</StackLayout>

提示:如果 IsReadOnly 属性在部分平台不生效,优先使用自定义 Handler 的方式,因为不同平台原生控件行为存在差异,Handler 能更精准地控制输入权限。

内容的提问来源于stack exchange,提问作者Anxo Gómez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 02:52:22