Last active
January 7, 2019 17:57
-
-
Save LanceMcCarthy/b4952879f94c6bba8bae74b9ae6b707d to your computer and use it in GitHub Desktop.
A Xamarin.Forms Effect where you can subscribe to an event handler when the keyboard is shown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomKeyboardEventArgs : EventArgs | |
{ | |
public CustomKeyboardEventArgs(Rectangle frameStart, Rectangle frameEnd) | |
{ | |
this.FrameStart = frameStart; | |
this.FrameEnd = frameEnd; | |
} | |
public Rectangle FrameStart { get; } | |
public Rectangle FrameEnd { get; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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()); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<conversationalUi:RadChat ...> | |
<conversationalUi:RadChat.Effects> | |
<effects:KeyboardNotificationEffect KeyboardShown="KeyboardNotificationEffect_OnKeyboardShown" KeyboardHidden="KeyboardNotificationEffect_OnKeyboardHidden"/> | |
</conversationalUi:RadChat.Effects> | |
</conversationalUi:RadChat> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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