xamarin.forms - Xamarin Forms - 防止键盘在 UWP、Android、iOS 中的条目焦点上显示
问题描述
在 Xamarin.Forms 中,我可以通过创建自定义渲染器并使用适用于 Android 的 ShowSoftInputOnFocus 和适用于 iOS 的 InputView 来防止在条目视图获得焦点时弹出键盘。
但是在 UWP 中我可以用什么来防止它呢?
解决方案
防止在条目视图获得焦点时弹出键盘
UWP 有直接的 API 支持来隐藏和显示InputPane。您可以调用TryHide
方法来隐藏键盘。对于 xamarin,您可以使用DependencyService
来接近。有关更多信息,请参阅以下代码。
界面
public interface IKeyboard
{
void HideKeyboard();
void ShowKeyboard();
void RegisterAction(Action<object, KeyboardState> callback);
}
public enum KeyboardState
{
Hide,
Show
}
键盘实现.cs
public class KeyboardImplementation : IKeyboard
{
private InputPane _inputPane;
private Action<object, KeyboardState> action;
public KeyboardImplementation()
{
_inputPane = InputPane.GetForCurrentView();
_inputPane.Showing += OnInputPaneShowing;
_inputPane.Hiding += OnInputPaneHiding;
}
public void HideKeyboard()
{
_inputPane.TryHide();
}
public void ShowKeyboard()
{
_inputPane.TryShow();
}
public void RegisterAction(Action<object, KeyboardState> callback)
{
action = callback;
}
private void OnInputPaneHiding(InputPane sender, InputPaneVisibilityEventArgs args)
{
action(this, KeyboardState.Hide);
}
private void OnInputPaneShowing(InputPane sender, InputPaneVisibilityEventArgs args)
{
action(this, KeyboardState.Show);
}
}
用法
DependencyService.Get<IKeyboard>().RegisterAction((s,e)=> {
if (e == KeyboardState.Show)
{
var keyboard = s as IKeyboard;
keyboard.HideKeyboard();
}
});
推荐阅读
- python - 如何从 python 进程调用 python 脚本
- jquery - Can I hide only the Bootstrap Modal's content?
- android - 多个连续的权限请求不起作用
- flutter - 如何移动下拉箭头左侧而不是默认情况下颤动的右侧?
- c# - 即使传递了值,标签也不会更新
- php - 如何使 Laravel 会话数组推送?
- php - 基于 PDO 的查询,标准字符串中有空格在 PHP 中不起作用
- reactjs - 在 bottomTabNavigator 上重新渲染 web 视图
- django - Django 视图在使用用户定义的装饰器检查会话时给出了 Forbidden 错误(CSRF 令牌丢失或不正确。)
- .net - 删除 Migrations 文件夹后的 EntityFramework Core 问题