How to programmatically show keyboard for text field?

I have one text box on a Windows phone page and I want to show the keyboard as soon as the page loads.

Is there a way to make this text box already focused when I go to this page?

I reviewed usage Guide.BeginShowKeyboardInput(), but I don't think this is a good solution in silverlight.

+5
source share
3 answers

Yes, I would not show the keyboard manually. This can annoy those who have devices with a physical keyboard. In the page load event, you can simply call the Focus method in the text box that you want to select. The keyboard should automatically display as needed.

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
            txtLongitude.Focus();
}
+8

OnNavigatedTo .

protected override void OnNavigatedTo(NavigationEventArgs e)
{
  base.OnNavigatedTo(e);

  // Set focus to the TextBox, this will pop up the 
  // virtual keyboard
  myTextBox.Focus();
}
+6

You can call textBox.Focus()through Dispatcherif you use it in an event OnNavigatedTo:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    if (e.NavigationMode != NavigationMode.Back)
    {
        Dispatcher.InvokeAsync(() => ShowKeyboard());
    }
}

private void ShowKeyboard()
{
    textBox.Focus();
}

In the instructions, ifmake sure that the keyboard is displayed only when the page does not move using the "Back" button.

0
source

All Articles