Windows Phone: re-layout after software keyboard is shown

旧城冷巷雨未停 提交于 2019-12-24 03:19:01

问题


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.



来源:https://stackoverflow.com/questions/27827874/windows-phone-re-layout-after-software-keyboard-is-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!