Prevent page screen scrolling on keyboard open (C # -XAML-Windows Store app)

Usually, when TextBox

focusing, the soft keyboard will open, but the problem is that by default, the page shift is up when the keyboard is opened, and it shifts back to the previous position when the user closes the keyboard by pressing the screen.

I need the page to switch to a specific Y. And I need to close the keyboard when I press the Done button, not only when I tap the screen.

I'm trying to find the default ScrollViewer

page that scrolls: Two events in a 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 app to the simulator with 10.6" 1024x768 (4:3 100%)

and 10.6" 1366x768 (16:9 100%)

, it works great and the keyboard is shown in such a way that the ScrollViewer scrolls with the TextBox in the correct position, for those two resolutions only.

The problem is, I have to make it work the same on all screen resolutions. Any help how can I make my code dynamic for all screen sizes?

+3


source to share


1 answer


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

+1


source







All Articles