問題
UWPアプリで、ソフトキーボードをXaml上から非表示にしたい
例)・検索用のテキストボックス上でEnterキーを押した時
・検索実行ボタンを押した時
対応
UWPでは、Windows.UI.ViewManagement.InputPane を通じてソフトキーボードの表示状態をコントロールできます。
以下のような「ソフトキーボードを消すアクション」を作って、TextBoxやButtonへの入力に反応して実行できるようにします。
HideInputPaneAction.cs
public class HideInputPaneAction : DependencyObject, IAction
{
public object Execute(object sender, object parameter)
{
return Windows.UI.ViewManagement.InputPane.GetForCurrentView().TryHide();
}
}
Xaml側では「Enterキー押下イベントを補足するビヘイビア」や「検索ボタンの押下」をトリガーにして HideInputPaneAction を実行させます。
検索UI.xaml
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding SearchText.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
InputScope="Search"
>
<i:Interaction.Behaviors>
<mybehavior:KeyboardTrigger Key="Enter">
<mybehavior:KeyboardTrigger.Actions>
<core:InvokeCommandAction Command="{Binding DoSearchCommand}" />
<mybehavior:HideInputPaneAction />
</mybehavior:KeyboardTrigger.Actions>
</mybehavior:KeyboardTrigger>
</i:Interaction.Behaviors>
</TextBox>
<Button Content="検索"
Margin="8 0"
Width="80"
>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<core:InvokeCommandAction Command="{Binding DoSearchCommand}" />
<mybehavior:HideInputPaneAction />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Button>
</StackPanel>