Reputation: 1389
Generally when TextBox
is focused, soft-keyboard will be open, but the problem is that by default the page shift upwards when the keyboard is opened and again shift previous position when user closes the keyboard by tapping on the screen.
I need the page to shift to a certain Y. And I need to be able to close the keyboard when I click a "Done" Button not only when I tap on the screen.
I am trying to find the Default ScrollViewer
of the Page that is scrolling:
Two events on the TextBox:
private void PNotesTextBox_GotFocus(object sender, RoutedEventArgs e)
{
var parentScrollViewer = FindParent<ScrollViewer>(this);
parentScrollViewer.VerticalScrollMode = ScrollMode.Enabled;
parentScrollViewer.ChangeView(null, offset, null, true);
parentScrollViewer.UpdateLayout();
}
private void PNotesTextBox_Tapped(object sender, TappedRoutedEventArgs e)
{
offset = e.GetPosition(PNotesTextBox).Y - 10;
}
In the MainPage Constructor:
var inputPane = InputPane.GetForCurrentView();
inputPane.Showing += OnShowingInputPane;
private async void OnShowingInputPane(InputPane sender, InputPaneVisibilityEventArgs args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
var parentScrollViewer = FindParent<ScrollViewer>(this);
parentScrollViewer.VerticalScrollMode = ScrollMode.Enabled;
offset = 580 - args.OccludedRect.Top;
parentScrollViewer.ChangeView(null, offset, null, true);
parentScrollViewer.UpdateLayout();
});
}
public static T FindParent<T>(FrameworkElement reference)
where T : FrameworkElement
{
FrameworkElement parent = reference;
while (parent != null)
{
parent = parent.Parent as FrameworkElement;
var rc = parent as T;
if (rc != null)
{
return rc;
}
}
return null;
}
When I deploy my application on the simulator with 10.6" 1024x768 (4:3 100%)
and 10.6" 1366x768 (16:9 100%)
it works perfectly and the keyboard is shown in a way the ScrollViewer is scrolled with the TextBox in the right position, only for these two resolutions.
The problem is that I should make it work the same way on all the Screens resolutions. Any help how can I make my code dynamic for all screens sizes?
Upvotes: 3
Views: 1184
Reputation: 857
From the documentation
When the touch keyboard appears, it automatically repositions your UI to ensure that the focused element remains visible. This can cause other important areas of your UI to move off screen. However, you can disable the default behavior and make your own UI adjustments when the touch keyboard appears
Key presses on the touch keyboard raise KeyDown and KeyUp events just like key presses on hardware keyboards. However, the touch keyboard will not raise input events for Ctrl+A, Ctrl+Z, Ctrl+X, Ctrl+C, and Ctrl+V, which are reserved for text manipulation in the input control
There is a pretty decent example here
Upvotes: 1