Xamarin Forms-防止键盘在UWP,Android,iOS的输入焦点上显示 [英] Xamarin Forms - Prevent Keyboard from Showing on Entry Focus in UWP, Android, iOS

查看:256
本文介绍了Xamarin Forms-防止键盘在UWP,Android,iOS的输入焦点上显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xamarin.Forms中,我可以通过创建自定义渲染器并使用适用于Android的ShowSoftInputOnFocus和适用于iOS的InputView来防止Entry视图获得焦点时弹出键盘.

In Xamarin.Forms, I can prevent keyboard from popping up when Entry view receives focus by creating custom renderers and using ShowSoftInputOnFocus for Android and InputView for iOS.

但是在UWP中我可以用来阻止它吗?

But what can I use to prevent it in UWP?

推荐答案

防止输入"视图获得焦点时弹出键盘

prevent keyboard from popping up when Entry view receives focus

UWP具有直接的API支持,可以隐藏和显示 InputPane .您可以调用TryHide方法隐藏键盘.对于xamarin,您可以使用DependencyService进行处理.有关更多信息,请参考以下代码.

UWP has direct API support to hide and show the InputPane. You could invoke TryHide method to hide keyboard. For xamarin you could use DependencyService to approach. For more please refer the following code.

界面

public interface IKeyboard
{
    void HideKeyboard();
    void ShowKeyboard();
    void RegisterAction(Action<object, KeyboardState> callback);
}
public enum KeyboardState
{
    Hide,
    Show
}

KeyboardImplementation.cs

public class KeyboardImplementation : IKeyboard
{
    private InputPane _inputPane;
    private Action<object, KeyboardState> action;

    public KeyboardImplementation()
    {
        _inputPane = InputPane.GetForCurrentView();
        _inputPane.Showing += OnInputPaneShowing;
        _inputPane.Hiding += OnInputPaneHiding;
    }
    public void HideKeyboard()
    {
        _inputPane.TryHide();
    }
    public void ShowKeyboard()
    {
        _inputPane.TryShow();
    }
    public void RegisterAction(Action<object, KeyboardState> callback)
    {
        action = callback;
    }

    private void OnInputPaneHiding(InputPane sender, InputPaneVisibilityEventArgs args)
    {
        action(this, KeyboardState.Hide);
    }

    private void OnInputPaneShowing(InputPane sender, InputPaneVisibilityEventArgs args)
    {
        action(this, KeyboardState.Show);
    }
}

用法

DependencyService.Get<IKeyboard>().RegisterAction((s,e)=> {
    if (e == KeyboardState.Show)
    {
        var keyboard = s as IKeyboard;
        keyboard.HideKeyboard();
    }
});

这篇关于Xamarin Forms-防止键盘在UWP,Android,iOS的输入焦点上显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆