.NET MAUI:打开FlyoutPage时自动隐藏键盘的实现需求
解决FlyoutPage打开时自动关闭软键盘的问题
核心思路
监听FlyoutPage的IsPresented属性状态,当属性切换为true(侧边栏展开)时,强制隐藏当前页面的软键盘。
实现方案
方案一:直接在FlyoutPage后台代码处理
在你的FlyoutPage的.xaml.cs文件中添加属性变更监听,触发键盘隐藏逻辑:
public partial class MainFlyoutPage : FlyoutPage { public MainFlyoutPage() { InitializeComponent(); PropertyChanged += MainFlyoutPage_PropertyChanged; } private void MainFlyoutPage_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == nameof(IsPresented) && IsPresented) { HideSoftKeyboard(); } } private void HideSoftKeyboard() { // 分平台实现键盘隐藏 #if ANDROID var inputMethodManager = Android.App.Application.Context.GetSystemService(Android.Content.Context.InputMethodService) as Android.Views.InputMethods.InputMethodManager; var currentFocus = Android.App.Application.Context.CurrentActivity?.CurrentFocus; if (currentFocus != null) { inputMethodManager.HideSoftInputFromWindow(currentFocus.WindowToken, 0); currentFocus.ClearFocus(); } #elif IOS UIKit.UIApplication.SharedApplication.KeyWindow?.EndEditing(true); #elif WINDOWS Microsoft.UI.Xaml.Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, 0); Microsoft.UI.Xaml.Input.FocusManager.TryMoveFocus(Microsoft.UI.Xaml.Input.FocusNavigationDirection.Next); #endif } }
方案二:用Behavior实现解耦复用
如果希望页面代码更简洁,可封装成Behavior组件:
public class HideKeyboardOnFlyoutOpenBehavior : Behavior<FlyoutPage> { protected override void OnAttachedTo(FlyoutPage bindable) { base.OnAttachedTo(bindable); bindable.PropertyChanged += Bindable_PropertyChanged; } protected override void OnDetachingFrom(FlyoutPage bindable) { base.OnDetachingFrom(bindable); bindable.PropertyChanged -= Bindable_PropertyChanged; } private void Bindable_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var flyoutPage = sender as FlyoutPage; if (e.PropertyName == nameof(FlyoutPage.IsPresented) && flyoutPage.IsPresented) { HideSoftKeyboard(); } } private void HideSoftKeyboard() { // 同方案一的平台键盘隐藏逻辑 #if ANDROID var inputMethodManager = Android.App.Application.Context.GetSystemService(Android.Content.Context.InputMethodService) as Android.Views.InputMethods.InputMethodManager; var currentFocus = Android.App.Application.Context.CurrentActivity?.CurrentFocus; if (currentFocus != null) { inputMethodManager.HideSoftInputFromWindow(currentFocus.WindowToken, 0); currentFocus.ClearFocus(); } #elif IOS UIKit.UIApplication.SharedApplication.KeyWindow?.EndEditing(true); #elif WINDOWS Microsoft.UI.Xaml.Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, 0); Microsoft.UI.Xaml.Input.FocusManager.TryMoveFocus(Microsoft.UI.Xaml.Input.FocusNavigationDirection.Next); #endif } }
然后在XAML中引用该Behavior:
<FlyoutPage ...> <FlyoutPage.Behaviors> <local:HideKeyboardOnFlyoutOpenBehavior /> </FlyoutPage.Behaviors> <!-- 页面其余内容 --> </FlyoutPage>
注意事项
- Android平台额外清除了当前控件焦点,避免Entry重新获取焦点后再次弹出键盘
- 所有平台逻辑均为原生API调用,无第三方依赖,兼容性稳定
内容的提问来源于stack exchange,提问作者Dryzler
相关产品推荐
相关产品推荐

