【问题标题】:How to hide UWP application's keyboard programmatically?如何以编程方式隐藏 UWP 应用程序的键盘?
【发布时间】:2018-08-12 05:37:39
【问题描述】:

在我的 UWP (Xamarin Forms) 应用程序中,我有一个条形码扫描仪,每次扫描都会将文本填充到条目中。问题是,在我进行扫描后,键盘会随着条目的聚焦而弹出。

我想知道是否有一种方法可以隐藏软键盘,而无需绑定到条目的 FOCUSED 属性以手动将条目设置为未聚焦状态。有没有办法告诉操作系统隐藏键盘?我不确定这是否可能。

【问题讨论】:

    标签: xamarin xamarin.forms uwp xamarin.uwp


    【解决方案1】:

    您需要创建一个依赖服务并监听键盘出现时调用的事件。您可以像这样设置依赖服务:

    IKeyboard.cs(在您的 PCL 项目中):

    public interface IKeyboard
    {
        event EventHandler<EventArgs> KeyboardShowing;
        event EventHandler<EventArgs> KeyboardHiding;
        void HideKeyboard();
    }
    

    Keyboard_UWP.cs(在您的 UWP 项目中):

    public class Keyboard_UWP : IKeyboard
    {
        private InputPane _inputPane;
        public event EventHandler<double> KeyboardShowing;
        public event EventHandler<EventArgs> KeyboardHiding;
    
        public KeyboardVisibility_UWP()
        {
            _inputPane = InputPane.GetForCurrentView();
            _inputPane.Showing += OnInputPaneShowing;
            _inputPane.Hiding += OnInputPaneHiding;
        }
    
        private void OnInputPaneShowing(InputPane sender, InputPaneVisibilityEventArgs args)
        {
            KeyboardShowing?.Invoke(this, null);
        }
    
        private void OnInputPaneHiding(InputPane sender, InputPaneVisibilityEventArgs args)
        {
            KeyboardHiding?.Invoke(this, null);
        }
    
        public void HideKeyboard()
        {
            _inputPane.TryHide();
        }
    }
    

    然后在您的 PCL 中,您可以在它显示时收听:

    DependencyService.Get<IKeyboard>().KeyboardShowing += OnKeyboardShowing();
    
    private void OnKeyboardShowing(object sender, EventArgs e)
    {
        if (you want to hide the keyboard)
            DependencyService.Get<IKeyboard>().HideKeyboard();
    }
    

    【讨论】:

    • 感谢您的建议!我将对此进行测试并回复您! :)
    • 效果很好!谢谢!虽然,我一直在寻找一种可以像调用“DependencyService.Get().HideKeyboard();”一样简单地应用的解决方案无需在 VIew 后面编写代码,但这很好:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多