gpt4 book ai didi

xamarin.forms - Xamarin 表单检查键盘是否打开

转载 作者:行者123 更新时间:2023-12-04 17:15:56 25 4
gpt4 key购买 nike

有什么方法可以检查 Xamarin Forms 中的键盘是否打开?键盘打开或关闭时是否会触发任何事件?如果是这样,我在哪里可以找到它的示例?

最佳答案

我不相信有 Xamarin.Forms这样做的方式。无论如何,对于不同的平台(至少 Android 和 iOS),有一种方法可以实现您想要的。

安卓

android下有InputMethodManager类(class)。您可以从您的事件中获得它

var inputMethodManager = (InputMethodManager)this.GetSystemService(Context.InputMethodService);

现在您可以检查键盘是否显示为
var keyboardIsShown = inputMethodManager.IsAcceptingText;

根据 this article on CodeProject您可以使用派生自 IOnGlobalLayoutListener 的类监听全局布局事件。触发此事件后,您可以使用上面的代码检查是否由于键盘弹出而更改了布局。

iOS

在 iOS 下你可以使用 UIKeyboard允许您观察 DidShowNotification 的类(见 here)。
notification = UIKeyboard.Notifications.ObserveDidShow ((sender, args) => {
Debug.WriteLine("Keyboard is shown.");
// whatever
});

同样你可以观察到 DidHideNotification (和其他一些 - 见 here )。

Xamarin.Forms

要在 Xamarin.Forms 中实现键盘通知,最简单的方法是实现通过 DependencyService 解决的平台依赖关系。 .为此,您首先必须为平台服务引入一个接口(interface)。
public interface IKeyboardService
{
event EventHandler KeyboardIsShown;
event EventHandler KeyboardIsHidden;
}

在您的平台特定项目中,您必须以特定于平台的方式实现功能。有关 iOS 实现,请参阅以下代码部分
[assembly: Xamarin.Forms.Dependency(typeof(Your.iOS.Namespace.KeyboardService))]

namespace Your.iOS.Namespace
{
public class KeyboardService : IKeyboardService
{
public event EventHandler KeyboardIsShown;
public event EventHandler KeyboardIsHidden;

public KeyboardService()
{
SubscribeEvents();
}

private void SubscribeEvents()
{
UIKeyboard.Notifications.ObserveDidShow(OnKeyboardDidShow);
UIKeyboard.Notifications.ObserveDidHode(OnKeyboardDidHide);
}

private void OnKeyboardDidShow(object sender, EventArgs e)
{
KeyboardIsShown?.Invoke(this, EventArgs.Empty);
}

private void OnKeyboardDidHide(object sender, EventArgs e)
{
KeyboardIsHidden?.Invoke(this, EventArgs.Empty);
}
}
}
Xamarin.Forms.Dependency使类(class)对 DependencyService 可见. Android实现见以下代码
[assembly: Xamarin.Forms.Dependency(typeof(Your.Android.Namespace.KeyboardService))]

namespace Your.Android.Namespace
{
public class KeyboardService : IKeyboardService
{
public event EventHandler KeyboardIsShown;
public event EventHandler KeyboardIsHidden;

private InputMethodManager inputMethodManager;

private bool wasShown = false;

public KeyboardService()
{
GetInputMethodManager();
SubscribeEvents();
}

public void OnGlobalLayout(object sender, EventArgs args)
{
GetInputMethodManager();
if(!wasShown && IsCurrentlyShown())
{
KeyboardIsShown?.Invoke(this, EventArgs.Empty);
wasShown = true;
}
else if(wasShown && !IsCurrentlyShown())
{
KeyboardIsHidden?.Invoke(this, EventArgs.Empty);
wasShown = false;
}
}

private bool IsCurrentlyShown()
{
return inputMethodManager.IsAcceptingText;
}

private void GetInputMethodManager()
{
if (inputMethodManager == null || inputMethodManager.Handle == IntPtr.Zero)
{
inputMethodManager = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
}
}

private void SubscribeEvents()
{
((Activity)Xamarin.Forms.Forms.Context).Window.DecorView.ViewTreeObserver.GlobalLayout += this.OnGlobalLayout;
}
}
}

在您的 Xamarin.Forms 应用程序中,您现在可以获得 IKeyboardService 的正确实现的实例和
var keyboardService = Xamarin.Forms.DependencyService.Get<IKeyboardService>();  

关于xamarin.forms - Xamarin 表单检查键盘是否打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46360257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com