有文本框和自定义键盘。当 TextBox 必须获得焦点时,我需要禁用输入窗格。我尝试了 TryHide 方法来显示和聚焦事件。
InputPane.GetForCurrentView().TryHide();
但这是一个非常糟糕的解决方案,因为当用户点击 TextBox 时,InputPane 会闪烁。然后我在文档CoreTextInputPaneDisplayPolicy中发现了更改输入策略的可能性,但文档没有解释如何应用此策略。 使用 TextBlock 不适合我,因为我需要使用光标进行操作并选择文本。这个问题有好的解决办法吗?
Microsoft 在此处演示了代码示例。
// Create a CoreTextEditContext for our custom edit control.
CoreTextServicesManager manager = CoreTextServicesManager.GetForCurrentView();
_editContext = manager.CreateEditContext();
// Get the Input Pane so we can programmatically hide and show it.
_inputPane = InputPane.GetForCurrentView();
// For demonstration purposes, this sample sets the Input Pane display policy to Manual
// so that it can manually show the software keyboard when the control gains focus and
// dismiss it when the control loses focus. If you leave the policy as Automatic, then
// the system will hide and show the Input Pane for you. Note that on Desktop, you will
// need to implement the UIA text pattern to get expected automatic behavior.
_editContext.InputPaneDisplayPolicy = CoreTextInputPaneDisplayPolicy.Manual;
我一直在 WinUI 3(Windows App SDK)应用程序中遇到同样的问题。
我已经用下面的代码成功解决了这个问题。它也应该适用于 UWP。它可以防止键盘完全显示,没有闪烁/闪烁。
public sealed partial class CustomTextBox : TextBox
{
public CustomTextBox()
{
InitializeComponent();
CoreInputView.GetForCurrentView().PrimaryViewShowing += (sender, args) =>
{
args.TryCancel();
sender.TryHide();
};
}
...
}
args.TryCancel()
单独完成这项工作,但是当应用程序失去焦点时,键盘就会出现,因此需要 sender.TryHide()
来消除这种难看的行为。