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

时间:2015-01-07 20:14:40

标签: c++ windows-phone

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

1 个答案:

答案 0 :(得分:2)

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

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>

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

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

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