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

MAUI中键盘弹出界面上移:如何关闭该自动上移行为?

解决Xamarin.Forms键盘弹出时界面自动上移的问题

针对你遇到的Grid星号高度布局下,Entry获取焦点后界面随键盘上移的问题,有以下几种可行方案:

1. 使用Xamarin.Forms内置属性(推荐,5.0+版本支持)

直接在页面的XAML中设置KeyboardAutoAdjustmentBehavior属性为Never,即可完全禁止键盘弹出时的界面自动调整行为,同时保留Grid的自适应布局:

<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="YourApp.YourPage"
    KeyboardAutoAdjustmentBehavior="Never">
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Entry Placeholder="Write text..."/>
        <Button Text="Button" Grid.Row="1" VerticalOptions="End" />
    </Grid>
</ContentPage>

KeyboardAutoAdjustmentBehavior的可选值说明:

  • Resize:默认行为,调整界面布局以适应键盘
  • Pan:平移界面而非调整布局
  • Never:完全不调整界面

2. 旧版本Xamarin.Forms的平台特定解决方案

如果你的项目使用低于5.0的Xamarin.Forms版本,需要通过自定义渲染器实现:

Android平台

创建自定义页面渲染器:

[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))]
namespace YourApp.Droid
{
    public class CustomPageRenderer : PageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);
            
            if (e.NewElement != null)
            {
                Context.GetActivity().Window.SetSoftInputMode(Android.Views.SoftInput.AdjustNothing);
            }
        }
    }
}

iOS平台

创建自定义页面渲染器:

[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))]
namespace YourApp.iOS
{
    public class CustomPageRenderer : PageRenderer
    {
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            
            if (ViewController != null)
            {
                ViewController.AutomaticallyAdjustsScrollViewInsets = false;
            }
        }
    }
}

以上方案都能在保留Grid星号高度自适应的前提下,关闭键盘弹出时的界面上移行为。

内容的提问来源于stack exchange,提问作者Silny ToJa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 17:47:25