【问题标题】:UWP - How to prevent InputPane from showing and hiding automaticallyUWP - 如何防止 InputPane 自动显示和隐藏
【发布时间】:2017-03-27 01:37:04
【问题描述】:

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

在我将其图像放在顶部的页面中,我希望 InputPane 在用户导航到页面时显示并继续显示,直到他/她单击指定的按钮,并防止在用户单击页面中的其他任何位置时隐藏它。

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

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

【问题讨论】:

  • 据我所知,无法更改默认隐藏行为。
  • @ElvisXia-MSFT 感谢重播。
  • @jerrynixon 你对此有什么建议吗?

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


【解决方案1】:

控制它的简单方法是控制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);
            }
        }
    }
}

【讨论】:

  • 我运行了你的代码,TextBox 专注于触摸和点击。如果我使用 TextBlock,那么我需要为它实现一个假光标。
猜你喜欢
  • 2023-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多