.NET MAUI中CommunityToolkit.Maui的KeyboardExtensions遮挡Entry问题排查
.NET MAUI中KeyboardExtensions导致Entry被软键盘部分遮挡的问题排查
我在基于.NET 8的.NET MAUI应用中使用CommunityToolkit.Maui(版本7.0.0)的KeyboardExtensions,用于避免软键盘遮挡用户控件。该功能虽有效果,但Entry仍会被软键盘部分遮挡。我曾怀疑是Entry外层的Border导致的视觉问题,但移除Border后问题依旧。点击密码Entry时也会出现类似的半遮挡情况。
使用环境:Visual Studio 17.8.0 + .NET 8正式版
LoginPage.xaml代码
<!-- Login Form --> <Grid RowDefinitions="*,60,60,100,50" Padding="0"> <!-- Background Image --> <Image Grid.RowSpan="5" Source="teton_snake_dimmed.jpg" Aspect="AspectFill" /> <!-- 0: Logo --> <Image Grid.Row="0" Source="ingrid_connect_logo.png" HeightRequest="130" HorizontalOptions="Center" VerticalOptions="Center" /> <!-- 1: Username Entry --> <Border Grid.Row="1" BackgroundColor="{StaticResource Gray100}" Padding="5" HorizontalOptions="Center" VerticalOptions="Center"> <Border.StrokeShape> <RoundRectangle CornerRadius="5,5,5,5" /> </Border.StrokeShape> <Entry Text="{Binding UserName}" TextColor="{StaticResource Black}" BackgroundColor="White" Placeholder="Email Address" Focused="ShowKeyboard" Unfocused="HideKeyboard" Keyboard="Email" WidthRequest="350" /> </Border> <!-- 2: Password Entry --> <Border Grid.Row="2" BackgroundColor="{StaticResource Gray100}" Padding="5" HorizontalOptions="Center" VerticalOptions="Center"> <Border.StrokeShape> <RoundRectangle CornerRadius="5,5,5,5" /> </Border.StrokeShape> <Entry Text="{Binding Password}" TextColor="{StaticResource Black}" BackgroundColor="White" IsPassword="True" Placeholder="Password" Focused="ShowKeyboard" Unfocused="HideKeyboard" WidthRequest="350" /> </Border> <!-- 3: Submit Button --> <Button Grid.Row="3" BackgroundColor="{StaticResource Primary}" Text="S I G N I N" FontSize="13" FontAttributes="Bold" Command="{Binding SubmitCommand}" HeightRequest="50" WidthRequest="350" HorizontalOptions="Center" VerticalOptions="Center" /> <!-- 4: Register/Forgot Password Links --> <HorizontalStackLayout Grid.Row="4" HorizontalOptions="Center" VerticalOptions="Center"> <!-- New User Sign Up --> <Border BackgroundColor="Transparent" StrokeThickness="0" Padding="7"> <Border.GestureRecognizers> <TapGestureRecognizer Command="{Binding GoToSignUpCommand}"/> </Border.GestureRecognizers> <Label Text="New User Sign Up" TextColor="White" FontSize="12" /> </Border> <!-- Seperator --> <Border BackgroundColor="Transparent" StrokeThickness="0" Padding="7"> <Label Text="|" TextColor="White" FontSize="12" Margin="5,0,5,0" /> </Border> <!-- Forgot Password --> <Border BackgroundColor="Transparent" StrokeThickness="0" Padding="7"> <Border.GestureRecognizers> <TapGestureRecognizer Command="{Binding GoToForgotPasswordCommand}"/> </Border.GestureRecognizers> <Label Text="Forgot Password" TextColor="White" FontSize="12" /> </Border> </HorizontalStackLayout> </Grid>
后台代码
private async void ShowKeyboard(object sender, FocusEventArgs e) { var entry = (Entry)sender; if (!KeyboardExtensions.IsSoftKeyboardShowing(entry)) await KeyboardExtensions.ShowKeyboardAsync(entry, default); } private async void HideKeyboard(object sender, FocusEventArgs e) { var entry = (Entry)sender; if (KeyboardExtensions.IsSoftKeyboardShowing(entry)) await KeyboardExtensions.HideKeyboardAsync(entry, default); }
请问这是布局问题,还是CommunityToolkit.Maui中KeyboardExtensions的问题?
内容的提问来源于stack exchange,提问作者Sam
相关产品推荐
相关产品推荐

