This plugin includes:
Xamarin.KeyboardHelper
Available on NuGet: https://www.nuget.org/packages/Xamarin.KeyboardHelper xmlns:effects="clr-namespace:Xamarin.KeyboardHelper;assembly=Xamarin.KeyboardHelper"
at the top of the xaml file Platform | Supported | Version | Notes |
---|---|---|---|
Xamarin.iOS | Yes | iOS 10+ | |
Xamarin.Android | Yes | API 19+ | Project should target Android framework 9.0+ |
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
//need this line to init effect in android
Xamarin.KeyboardHelper.Platform.Droid.Effects.Init(this);
LoadApplication(new App());
}
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
//need this line to init effect in iOS
Xamarin.KeyboardHelper.Platform.iOS.Effects.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
<Entry Text="Show Keyboard" effects:KeyboardEffect.EnableKeyboard="True">
<Entry.Effects>
<effects:KeyboardEnableEffect/>
</Entry.Effects>
</Entry>
<Entry Text="Hide Keyboard" effects:KeyboardEffect.EnableKeyboard="False">
<Entry.Effects>
<effects:KeyboardEnableEffect/>
</Entry.Effects>
</Entry>
<Entry Text="Toggle Keyboard" effects:KeyboardEffect.EnableKeyboard="{Binding BooleanBinding}">
<Entry.Effects>
<effects:KeyboardEnableEffect/>
</Entry.Effects>
</Entry>
In the previous version of the plugin, control that uses the effect will automatically get the focus when view get rendered. In version 2.0.5 and above, control will not automatically get focus anymore, instead if you want to get focus, you have to call the RequestFocus = true in your XAML file.
<Entry effects:KeyboardEffect.EnableKeyboard="False" effects:KeyboardEffect.RequestFocus="True">
<Entry.Effects>
<effects:KeyboardEnableEffect />
</Entry.Effects>
</Entry>
RequestFocus="True"
will not show the keyboard if EnableKeyboard="False"
Entry.Focus()
will shows the keyboard even if EnableKeyboard="False"
. but it will be hidden immediately after it is shown.RequestFocus="True"
do ?Calling Entry.Focus()
in page ViewIsAppearing will not focus on the entry. RequestFocus="True"
will do that for you.
public MainPage()
{
InitializeComponent();
this.Appearing += MainPage_Appearing;
this.Disappearing += MainPage_Disappearing;
}
private void MainPage_Disappearing(object sender, EventArgs e)
{
SoftKeyboard.Current.VisibilityChanged -= Current_VisibilityChanged;
}
private void MainPage_Appearing(object sender, EventArgs e)
{
SoftKeyboard.Current.VisibilityChanged += Current_VisibilityChanged;
}
private void Current_VisibilityChanged(SoftKeyboardEventArgs e)
{
if(e.IsVisible){
// do your things
}
else{
// do your things
}
}
Contributions are welcome. Feel free to file issues and pull requests on the repo and they'll be reviewed as time permits.
Under MIT, see LICENSE file.