Windows Phone:显示软件键盘后重新布局

Windows Phone: re-layout after software keyboard is shown

本文关键字:新布局 布局 Phone 显示 软件 键盘 Windows      更新时间:2023-10-16

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

似乎需要注册处理程序才能更新布局:

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

为了处理事件,我们可以在 event 参数中使用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>

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

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

尽管应该很简单,但当显示/隐藏键盘时,将调用处理程序,并设置(或隐藏)虚拟网格以占用显示键盘的空间。