Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Xamarin.Forms Effect where you can subscribe to an event handler when the keyboard is shown
public static class ConversionHelpers
{
public static Xamarin.Forms.Rectangle AsRectangle(this CGRect o)
{
return new Xamarin.Forms.Rectangle
{
Height = o.Height,
Width = o.Width,
X = o.X,
Y = o.Y,
Bottom = o.Bottom,
Top = o.Top,
Left = o.Left,
Location = o.Location.ToPoint()
};
}
}
public class CustomKeyboardEventArgs : EventArgs
{
public CustomKeyboardEventArgs(Rectangle frameStart, Rectangle frameEnd)
{
this.FrameStart = frameStart;
this.FrameEnd = frameEnd;
}
public Rectangle FrameStart { get; }
public Rectangle FrameEnd { get; }
}
public class KeyboardNotificationEffect : RoutingEffect
{
public event EventHandler<CustomKeyboardEventArgs> KeyboardShown;
public event EventHandler<CustomKeyboardEventArgs> KeyboardHidden;
public KeyboardNotificationEffect()
: base("Telerik.KeyboardNotificationEffect")
{ }
public void InvokeShown(Rectangle frameStart, Rectangle frameEnd)
{
KeyboardShown?.Invoke(this, new CustomKeyboardEventArgs(frameStart, frameEnd));
}
public void InvokeHidden(Rectangle frameStart, Rectangle frameEnd)
{
KeyboardHidden?.Invoke(this, new CustomKeyboardEventArgs(frameStart, frameEnd));
}
}
[assembly: ResolutionGroupName("Telerik")]
[assembly: ExportEffect(typeof(YourProject.iOS.Effects.KeyboardNotificationEffect), "KeyboardNotificationEffect")]
namespace YourProject.iOS.Effects
{
public class KeyboardNotificationEffect : PlatformEffect
{
protected override void OnAttached()
{
UIKeyboard.Notifications.ObserveWillShow(UIKeyboard_WillShow);
UIKeyboard.Notifications.ObserveWillHide(UIKeyboard_WillHide);
}
protected override void OnDetached()
{
}
private void UIKeyboard_WillHide(object sender, UIKeyboardEventArgs e)
{
if (Element.Effects.FirstOrDefault(ef => ef is ArtGalleryCRM.Forms.Effects.KeyboardNotificationEffect) is ArtGalleryCRM.Forms.Effects.KeyboardNotificationEffect effect)
{
effect.InvokeHidden(e.FrameBegin.AsRectangle(), e.FrameEnd.AsRectangle());
}
}
private void UIKeyboard_WillShow(object sender, UIKeyboardEventArgs e)
{
if (Element.Effects.FirstOrDefault(ef => ef is ArtGalleryCRM.Forms.Effects.KeyboardNotificationEffect) is ArtGalleryCRM.Forms.Effects.KeyboardNotificationEffect effect)
{
effect.InvokeShown(e.FrameBegin.AsRectangle(), e.FrameEnd.AsRectangle());
}
}
}
}
<conversationalUi:RadChat ...>
<conversationalUi:RadChat.Effects>
<effects:KeyboardNotificationEffect KeyboardShown="KeyboardNotificationEffect_OnKeyboardShown" KeyboardHidden="KeyboardNotificationEffect_OnKeyboardHidden"/>
</conversationalUi:RadChat.Effects>
</conversationalUi:RadChat>
private void KeyboardNotificationEffect_OnKeyboardShown(object sender, CustomKeyboardEventArgs e)
{
if (vm.Items.Count > 0)
{
var emptyMessage = vm.Items[0];
if (emptyMessage.Category == MessageCategory.Empty)
{
emptyMessage.EmptyHeight = (double)e.FrameEnd.Height;
}
}
}
private void KeyboardNotificationEffect_OnKeyboardHidden(object sender, CustomKeyboardEventArgs e)
{
if (vm.Items.Count > 0)
{
var emptyMessage = vm.Items[0];
if (emptyMessage.Category == MessageCategory.Empty)
{
emptyMessage.EmptyHeight = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.