0

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?

cd491415
  • 823
  • 2
  • 14
  • 33
  • Hi @cd491415 have you found any solution to prevent the soft keyboard popping up on entry focus for xamarin forms uwp? – Riyas May 09 '19 at 06:36

1 Answers1

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();
    }
});
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • @NicoShu Thanks Nico but this solution will show soft keyboard then hide it. I am looking for a solution to not show keyboard at all when Entry receives focus. I have found article explaining how to do that using custom entry renderer and ShowSoftInputOnFocus for Android and InputView on iOS but custom renderers do same, keyboard will still show up briefly, then go away. Same is which your suggestion which makes things even worse since user now keeps getting keyboard pop up then disappear by itself. – cd491415 Nov 23 '18 at 20:19
  • Yep, this solution is just used to hide keyboard and it is global setting. it could be flexible with making unsubscribe method in the interface. And there is another solution in uwp, please refer this [case reply](https://stackoverflow.com/a/39517700/7254781). if you think it is feasible, I could help make a plugin for xamarin `Entry` control. – Nico Zhu Nov 26 '18 at 02:29
  • Thanks @NicoShu but I am not looking for solution to hide keyboard. I am looking for solution to ***prevent*** keyboard from showing up entirely once custom Entry receives focus. Popping keyboard and then hiding it makes it even worse. – cd491415 Nov 26 '18 at 16:28
  • Yep, I know, but currently, there is no such property for xaml control. if you do want this feature, please feel free post it in [user voice](https://wpdev.uservoice.com/). – Nico Zhu Nov 27 '18 at 05:04
  • The link you provided has no area to request feature for Xamarin Forms at all?? – cd491415 Nov 27 '18 at 22:09
  • Because your requirement part is in uwp, so the above link is microsoft uservioce. For xamarin forms you could post your requirement in [github](https://github.com/xamarin/Xamarin.Forms). – Nico Zhu Nov 28 '18 at 01:50
  • I think this solution unavoidably leads to spaghetti code because next thinkg that will happen, you will have calls to Focus, HideKeyboard, Showkeyboard all over the code competing with each other. For a hello world, yes this will show keyboard then hide it and I guess that *smell* if good for you, will work. But it is obvious hack, your competition will come to you and say "hey, how come other guy's app does not pop the keyboard and hide it all the time??? It is ridiculous to have to do such hacks all over the Xamarin. If you find solution for this working on all 3 platforms please share – pixel Jun 19 '19 at 17:45