Xamarin Forms - Prevent Keyboard from Showing on Entry Focus in UWP, Android, iOS

倖福魔咒の 提交于 2020-01-16 19:07:10

问题


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.

But what can I use to prevent it in UWP?


回答1:


prevent keyboard from popping up when Entry view receives focus

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.

Interface

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

Usage

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


来源:https://stackoverflow.com/questions/53439208/xamarin-forms-prevent-keyboard-from-showing-on-entry-focus-in-uwp-android-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!