xamarin - 如何以编程方式隐藏 UWP 应用程序的键盘?

标签 xamarin xamarin.forms uwp xamarin.uwp

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

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

最佳答案

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

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();
}

关于xamarin - 如何以编程方式隐藏 UWP 应用程序的键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49095657/

相关文章:

android - Xamarin Google Service Auth list 上缺少 SignInHubActivity

xamarin - Xamarin Forms 是否准备好供生产使用

c# - 从代码设置绑定(bind)在 xamarin.forms 应用程序中不起作用

c# - 如何在一定时间后调用另一个 Activity

android - 在启动画面中插入加载环

xaml - 所有标签的自定义字体 Xamarin Forms

c++ - 从纯 C 使用 Windows 运行时 API

windows - 为什么 UWP 10 开发如此缓慢?

JavaScript 运行时错误 : Unable to get property 'activate' of undefined or null reference

c# - 将后台安全任务与 async/await 一起使用时的正确实现