LoginSignup
4
3

More than 5 years have passed since last update.

[UWP]ソフトキーボードを非表示にする方法

Last updated at Posted at 2016-09-12

問題

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>

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3