Windows Phone: re-layout after software keyboard i

2019-07-18 11:09发布

问题:

When the software keyboard is shown, it occludes part of the page UI. That is undesirable. Is there a way to automatically update the UI layout just like Android Activity's onConfigurationChanged?

回答1:

It appears that one needs to register handler to update the layout:

auto inputpane = InputPane::GetForCurrentView();
inputpane->Showing += ref new TypedEventHandler<InputPane^, InputPaneVisibilityEventArgs^>(this, &MainPage::OnInputPaneVisibilityChanged);
inputpane->Hiding += ref new TypedEventHandler<InputPane^, InputPaneVisibilityEventArgs^>(this, &MainPage::OnInputPaneVisibilityChanged);

To handle the event, we can make use of OccludedRect in the event argument from which we can extract the height taken by the keyboard. First, we preserve some UI element, say SpaceForKeyboard, in the XAML. For example:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    <Grid.RowDefinitions>
    <Grid Grid.Row="0"> <!-- Main UI goes here --> </Grid>
    <Grid Grid.Row="1" x:Name="SpaceForKeyboard"> <!-- Dummy grid for the keyboard --> </Grid>
</Grid>

Then in the handler, just change the size for the preserved space:

void MainPage::OnInputPaneVisibilityChanged(InputPane^ sender, InputPaneVisibilityEventArgs^ args)
{
    SpaceForKeyboard->Height = sender->OccludedRect.Height;
}

As simple as it should be, when the keyboard is shown/hidden, the handler is invoked and it sets (or hide) the dummy grid to occupy the space where the keyboard is shown.