Create an interface class
namespace EasyBridge_App.Interfaces
{
public interface IKeyboardHelper
{
void HideKeyboard();
}
}
Dependency class in Android
using Android.Content;
using Android.Runtime;
using Android.Views.InputMethods;
using EasyBridge_App.Droid.Helpers;
using EasyBridge_App.Interfaces;
using Xamarin.Essentials;
using Xamarin.Forms;
[assembly: Dependency(typeof(KeyboardHelper))]
namespace EasyBridge_App.Droid.Helpers
{
[Preserve(AllMembers = true)]
public class KeyboardHelper : IKeyboardHelper
{
public void HideKeyboard()
{
var context = Android.App.Application.Context;
var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
if (inputMethodManager != null)
{
var activity = Platform.CurrentActivity;
var token = activity.CurrentFocus?.WindowToken;
inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);
activity.Window.DecorView.ClearFocus();
}
}
}
}
Dependency class in iOS
using EasyBridge_App.Interfaces;
using EasyBridge_App.iOS.Helpers;
using Foundation;
using UIKit;
using Xamarin.Forms;
[assembly: Dependency(typeof(KeyboardHelper))]
namespace EasyBridge_App.iOS.Helpers
{
[Preserve(AllMembers = true)]
public class KeyboardHelper : IKeyboardHelper
{
public void HideKeyboard()
{
UIApplication.SharedApplication.KeyWindow.EndEditing(true);
}
}
}
Dependency class in UWP
using EasyBridge_App.Interfaces;
using EasyBridge_App.UWP.Helpers;
using Windows.UI.ViewManagement;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
[assembly: Dependency(typeof(KeyboardHelper))]
namespace EasyBridge_App.UWP.Helpers
{
[Preserve(AllMembers = true)]
public class KeyboardHelper : IKeyboardHelper
{
private InputPane _inputPane;
public KeyboardHelper()
{
_inputPane = InputPane.GetForCurrentView();
}
public void HideKeyboard()
{
_inputPane.TryHide();
}
}
}
Using in shared project
DependencyService.Get<IKeyboardHelper>().HideKeyboard();