I've been creating some Silverlight applications for Windows Phone lately and I've run into a few speed bumps I thought I'd pass along. This one is in regards to closing the SIP.
The virtual keyboard on the phone or the SIP (Soft Input Panel) is the keyboard that pops up on the screen so the user can input data. Getting the SIP to display is simple—just give the TextBox focus and the SIP will automatically display. The user can then go about tapping away and entering text. But how do you dismiss the SIP? Well, if the TextBox loses focus (the user taps on the screen outside the TextBox for example) the SIP will close. But how do you programmatically dismiss the SIP? Removing focus from the TextBox seems like the way to go and it is.
If you want to dismiss the SIP when the user taps a specific key, such as the Enter key, you can listen to the KeyUp event and set focus to a different Control. Okay, that seems simple. But suppose you have a basic app without another Control. Well, you always have one Control—the root PhoneApplicationPage control. You can set focus on this. But there is one gotcha that took me a little while to figure out—you have to set IsTabStop to true on the main Control. If you don’t set this property to true, the TextBox will not lose focus.
The following is some simple code to get this to work.
The XAML creates the TextBox with an event handler for the KeyUp event and it sets the InputScope to restrict the keyboard to only display digits. For more information on InputScope and SIP layout, see this MSDN article on How to: Specify the SIP Layout in a TextBox. An event handler for the Loaded event is also specified.
<Grid x:Name="ContentGrid" Grid.Row="1" Loaded="ContentGrid_Loaded">
<TextBox Name="Input" KeyUp="Input_KeyUp" FontSize="40">
<InputScopeName NameValue="Digits" />
Code Behind (C#)
The C# code behind creates event handlers for the Loaded event and the KeyUp event. In the Loaded event handler, IsTabStop on the main control is set to true and the TextBox is given focus (this will force the SIP to display when the application loads).
In the KeyUp event handler, Focus is set on the main control if the Key pressed is the Enter key. Depending on the logic you want to do when the Enter key is pressed, you may also want to listen for the LostFocus event and handle that as well.
private void Input_KeyUp(object sender, KeyEventArgs e)
if (e.Key == Key.Enter)
private void ContentGrid_Loaded(object sender, RoutedEventArgs e)
// Turn on Tab Stops. You can set this in XAML as well.
this.IsTabStop = true;
// Set focus on the TextBox.
Hope this helps.