Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent screen scrolling up when soft keyboard is shown in WP7?

Is there a way to prevent the screen to shift upwards when the soft keyboard is shown in a Windows Phone app?

I have a text box which is located in the upper half of the screen, so when the soft keyboard is shown, it doesn't cover the text box. However, when the textbox is tapped, the screen still shifts up.

Can this be prevented?

like image 811
Philippe Leybaert Avatar asked Apr 09 '12 15:04

Philippe Leybaert


3 Answers

I spent quite some time trying to find a solution to this - in my case I wanted the page header to remain at the top of the screen instead of scrolling away when the keyboard opened.

The best I found was the solution described in the post below, which uses changes in the frame's TranslateTransform (which is triggered by the keyboard opening / closing) to change the size of the top margin and therefore the space for available content.

http://sorokoletov.com/2011/08/windows-phone-70-handling-text-entry-screens/

If you have a screen with a number of elements, you may want to consider explicitly changing the layout to accommodate the smaller space.

like image 77
Ben Avatar answered Nov 09 '22 01:11

Ben


I'm not convinced there is an easy way to change the scroll position when the keyboard is opened. Though I can think of several workarounds:

  • put everything in a scrollviewer, when the textboxes focusevent is called scroll the scroll viewer to keep the item you are interested in in view using ScrollToVerticalOffset()

  • use the X component of a translate transform to move the textbox around on the screen. There is a good blog post about it from Alex Sorokoletov.

like image 39
gbanfill Avatar answered Nov 08 '22 23:11

gbanfill


I found this scrolling all the way to the bottom of all comments in the article cited above:

Scrollviewer and Multiline Textbox Windows Phone

It helped me better than Alex's article.

like image 3
Jakub Kaleta Avatar answered Nov 09 '22 01:11

Jakub Kaleta