Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard won't dismiss even after Focus change

Tags:

c#

windows

xaml

I am creating a Windows 8.1 app and after the user presses a button, a popup opens over most of the screen. There are several textboxes inside the popover.

I found this sample code from microsoft about how to detect the appearance of the on-screen keyboard.

I have also found the following SO posts and sites basically informing that there is no way to force the keyboard to close, and the correct thing to do is in fact programmatically focus a hidden element on the page or disable and then re-enable the textbox:

  • Forcing Windows 8 soft keyboard to hide
  • Windows 8 soft keyboard not hidden
  • Show/Hide Keyboard Automatically Widnows 8
  • How to Dismiss Touch Keyboard

So I followed the advice and created an invisible button. When the user taps the close button, it is supposed to give focus to that button and dismiss the keyboard. What happens is the textbox does lose focus, but the keyboard does not go away. If I cause the close button to give focus to the hidden button and close the popup (which is the desired effect), the keyboard does not go away until the view (that was previously under the popup) is tapped.

How can I make closing the popup cause the keyboard to dismiss?

EDIT: It appears that there might be a way to programmatically dismiss the keyboard because triggering the App Bar to open while the keyboardi s open automatically dismisses the keyboard.

like image 467
eliot Avatar asked Dec 31 '13 13:12

eliot


2 Answers

When the textbox that shows the virtual keyboard was disabled it will dismiss the virtual keyboard. so the solution is set the textbox property IsEnabled to false and set it again to true so it can be use again.

TextBox.KeyDown += (s, a) => {
 if (a.Key == VirtualKey.Enter) {
   TextBox.IsEnabled = false;
   TextBox.IsEnabled = true;
 }
like image 134
Paul Avatar answered Sep 21 '22 22:09

Paul


It was impossible to programmatically manage the touch-keyboard's appearance and disappearance. Unfortunately, changing the IsEnabled property didn't work for me.

The touch-keyboard appearance principle was known as Focus-driven, but I had walked out by setting the property IsTabStop=True on the UserControl explicitly. Besides, the TextBox won't activate the touch-keyboard if its IsTabStop=false.

In theory, I think the system searches the next potential TextBox, so that if so it wasn't to close and re-open, with touchable+inputable property. Maybe there were kind of bug that while releasing the Focus, current TextBox releases only his "touchable" focus, and didn't finish to release the keyboard's "inputable" focus, because that by default only the input-controls have Tab-Stoppable property.

By the way, if we close the UserControl by a CustomControl's Close button, the IsTabStop=true will be needed on his parent.

PS: Solution tested only on Windows 8.1 Store Application.

like image 20
Yang C Avatar answered Sep 21 '22 22:09

Yang C