Windows电话:显示软键盘后重新布局

Windows Phone: re-layout after software keyboard is shown

显示软键盘时,它会遮挡部分页面 UI。这是不可取的。有没有办法像 Android Activity 的 onConfigurationChanged 一样自动更新 UI 布局?

看来需要注册处理程序才能更新布局:

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

为了处理事件,我们可以在事件参数中使用 OccludedRect,我们可以从中提取键盘占用的高度。首先,我们在 XAML 中保留一些 UI 元素,例如 SpaceForKeyboard。例如:

<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>

然后在处理程序中,只需更改保留的大小 space:

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

应该很简单,当键盘为 shown/hidden 时,将调用处理程序并设置(或隐藏)虚拟网格以占据显示键盘的 space。