The code below works for me in a WinUI 3 (Windows App SDK) application. It should work for UWP as well. It completely prevents the keyboard from showing, without blinking/flickering.
public sealed partial class CustomTextBox
{
private void CustomTextBox_OnGotFocus(object sender, RoutedEventArgs e)
{
CoreInputView.GetForCurrentView().PrimaryViewShowing += BlockOnScreenKeyboard;
}
private void CustomTextBox_OnLostFocus(object sender, RoutedEventArgs e)
{
CoreInputView.GetForCurrentView().PrimaryViewShowing -= BlockOnScreenKeyboard;
}
private static void BlockOnScreenKeyboard(CoreInputView sender, CoreInputViewShowingEventArgs args)
{
args.TryCancel();
sender.TryHide();
}
...
}
`args.TryCancel()` does the job alone but when the app loses focus, the keyboard appears, so `sender.TryHide()` is needed to eliminate this bad-looking behavior.
I've been fighting the same problem in a WinUI 3 (Windows App SDK) application.
I've managed to solve it with the code below. It should work for UWP as well. It completely prevents the keyboard from showing, without blinking/flickering.
public sealed partial class CustomTextBox
{
private void CustomTextBox_OnGotFocus(object sender, RoutedEventArgs e)
{
CoreInputView.GetForCurrentView().PrimaryViewShowing += BlockOnScreenKeyboard;
}
private void CustomTextBox_OnLostFocus(object sender, RoutedEventArgs e)
{
CoreInputView.GetForCurrentView().PrimaryViewShowing -= BlockOnScreenKeyboard;
}
private static void BlockOnScreenKeyboard(CoreInputView sender, CoreInputViewShowingEventArgs args)
{
args.TryCancel();
sender.TryHide();
}
...
}
`args.TryCancel()` does the job alone but when the app loses focus, the keyboard appears, so `sender.TryHide()` is needed to eliminate this bad-looking behavior.