UWP - 如何防止InputPane自动显示和隐藏

时间:2016-11-13 07:04:46

标签: c# uwp winrt-xaml uwp-xaml on-screen-keyboard

我想手动控制InputPane的行为,以防止它自动显示或隐藏。

enter image description here

在我将其图片置于顶部的页面中,我希望InputPane显示为用户导航到该页面并一直显示,直到他/她点击指定按钮并防止其隐藏,如果用户点击页面中的任何其他位置。 / p>

此外,即使用户点击TextBox,我也希望InputPane保持隐藏状态。

我已经知道有TryShow()和TryHide(),但我无法自动显示和隐藏。

1 个答案:

答案 0 :(得分:0)

控制它的简便方法是控制TextBox的焦点。如果将TextBox上的IsTabStop设置为false - 它将不会占用焦点,因此SIP不会显示。如果它已经有焦点 - 你需要把它移出去。如果要显示SIP - 请关注TextBox。请注意,出于性能原因以及防止用户混淆 - 当控件不可编辑时,使用TextBlock代替TextBox可能是有意义的。

<强> XAML

<Page
    x:Class="App18.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App18"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition
                Height="Auto" />
        </Grid.RowDefinitions>
        <TextBox
            x:Name="myTextBox"
            IsTabStop="False"
            AcceptsReturn="True"
            VerticalAlignment="Stretch"
            TextChanged="MyTextBox_OnTextChanged"/>
        <Button
            x:Name="myButton"
            Grid.Row="1"
            Click="ButtonBase_OnClick">Edit</Button>
    </Grid>
</Page>

<强> C#

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace App18
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            myTextBox.IsTabStop = true;
            myTextBox.Focus(FocusState.Programmatic);
        }

        private void MyTextBox_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            if (myTextBox.Text.ToLower().Contains("done"))
            {
                myTextBox.IsTabStop = false;
                myButton.Focus(FocusState.Programmatic);
            }
        }
    }
}