locked
Set focus to control on page load

    Question

  • For a WP project I have build a login screen similar to the WP8.1 Wallet Pin. The page consists of no more than an image and a TextBox. The idea is when the page is loaded that the textbox gets focus and the software keyboard pops up beneath the textbox.

    This is in a simplified form what I have done:

    XAML:

    <Page x:Class="OnloadKeyboardControl.MainPage" ...
    Loaded="PhoneApplicationPage_Loaded" Tapped="PhoneApplicationPage_Loaded"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions>
    <Rectangle Grid.Row="0" Fill="Red"/> <Rectangle Grid.Row="1" Fill="Green"/> <TextBox Grid.Row="1" Name="TextBoxFoobar" FontSize="34" Text="Foobar" TextAlignment="Center" HorizontalAlignment="Center" InputScope="Number"/> </Grid> </Page>


    And in C# behind the page:

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
            {
                TextBoxFoobar.Focus(Windows.UI.Xaml.FocusState.Programmatic);
    
                Debug.WriteLine("Focus control attempted \t FocusState: {0} \t Source: {1}", TextBoxFoobar.FocusState, (e == null) ? "N/A" : e.ToString());
            }

    What happens is that the control get focus like it should, but the software keyboard overlaps the control instead of pushing it high enough to be above the keyboard. When I manually hide the keyboard (back button on the device) and I tap on the screen, the tapped action fires and executes the exact same method but this time the keyboard does push the control so it is still visible.

    Output of debug lines:

    Focus control attempted FocusState: Pointer Source: Windows.UI.Xaml.RoutedEventArgs Focus control attempted FocusState: Pointer Source: Windows.UI.Xaml.Input.TappedRoutedEventArgs

    The test solution can be found here: (Sorry apparently I cannot insert direct hyperlinks since I'm new here) 

    https://onedrive.live.com/redir?resid=D94AA58551FD1230!195401&authkey=!AEZHeKoR082IeBI&ithint=file%2czip

    Is there anyone who can help me?


    Saturday, November 29, 2014 7:57 PM

Answers

  • Hi Marijn,

    I think I found the problem. It caused by the display time of the keyboard. System showed it before the page rendered the TextBox control, so overlaid the TextBox control.

    The easiest way to fix is to set a delay time on the top of Load Event handler like the following code snippet.

    await Task.Delay(500);
    TextBoxFoobar.Focus(Windows.UI.Xaml.FocusState.Programmatic);
    

    Please feel free to let me know if you have any concerns.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, December 1, 2014 7:39 AM
    Moderator

All replies

  • Hi Marijn,

    I think I found the problem. It caused by the display time of the keyboard. System showed it before the page rendered the TextBox control, so overlaid the TextBox control.

    The easiest way to fix is to set a delay time on the top of Load Event handler like the following code snippet.

    await Task.Delay(500);
    TextBoxFoobar.Focus(Windows.UI.Xaml.FocusState.Programmatic);
    

    Please feel free to let me know if you have any concerns.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, December 1, 2014 7:39 AM
    Moderator
  • Thank you Herro,

    Making a delay worked indeed, in my case I needed 1000 ms. It feels like an oversight in the keyboard class.

    The problem is not always present in my main application, and also occurs more/less on faster/slower devices.

    Link with the fix in a solution for anybody who might encounter the same:

    https://onedrive.live.com/redir?resid=D94AA58551FD1230!196029&authkey=!AFVyJQ3SL9c_vr0&ithint=file%2czip

    Monday, December 1, 2014 2:48 PM