Resize winrt page when on on screen keyboard showi

2019-04-12 04:15发布

问题:

I have a Windows 8.1 C# app that shows a page with a pretty big textbox (covering almost all of the page; its a writing app).

When the on screen keyboard shows up, it overlays half of the textbox. I would like to resize the textbox (or even the whole page) so it isnt covered by the keyboard.

I am now thrying to achieve this using the static InputPane and subscribing to its showing and hiding events. I then try to change the margins of my textbox using the occuled rectangle provided in the eventargs. This works, but since my page is still the height of the screen it scrolls it halfway to the bottom.

public MainPage()
{
    var inputPane = InputPane.GetForCurrentView();
    inputPane.Showing += this.InputPaneShowing;
    inputPane.Hiding += this.InputPaneHiding;
}

void InputPaneHiding(InputPane sender, InputPaneVisibilityEventArgs args)
{
    this.EditBox.Margin = new Thickness();
}

private void InputPaneShowing(InputPane sender, InputPaneVisibilityEventArgs args)
{
    this.EditBox.Margin = new Thickness(0, 0, 0, args.OccludedRect.Height);
}

While trying this out I'm getting the feeling this isnt the ideal solution, but I havent got a better idea. Ideally I'd think it would be something like the vertical split when you have to apps opened, but then horizontally with the keyboard at the bottom and the app only the size available above the keyboard.

Any idea if this is possible?

回答1:

Since you are handling the interface occlusion yourself you should set the property EnsuredFocusedElementInView from InputPane.Showing eventargs to true. It would prevent interface from automatically scrolling.

 private void InputPaneShowing(InputPane sender, InputPaneVisibilityEventArgs args)
 {
    args.EnsuredFocusedElementInView = true;
    this.EditBox.Margin = new Thickness(0, 0, 0, args.OccludedRect.Height);
 };