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.