Skip to content

Instantly share code, notes, and snippets.

@GioviQ
Created March 9, 2020 08:04
Show Gist options
  • Save GioviQ/1e73bc6f9df6ddf8f4e52d4ea5c8bac6 to your computer and use it in GitHub Desktop.
Save GioviQ/1e73bc6f9df6ddf8f4e52d4ea5c8bac6 to your computer and use it in GitHub Desktop.
ContentButton for Xamarin Forms
using System.Windows.Input;
using Xamarin.Forms;
namespace Xamarin.Controls
{
public class ContentButton : ContentView
{
private readonly TapGestureRecognizer _tapGestureRecognizer;
public ContentButton()
{
_tapGestureRecognizer = new TapGestureRecognizer();
GestureRecognizers.Add(_tapGestureRecognizer);
}
protected override void OnChildAdded(Element child)
{
base.OnChildAdded(child);
if (child is View childview)
{
childview.GestureRecognizers.Add(_tapGestureRecognizer);
}
}
public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand),
typeof(ContentButton), null, BindingMode.Default, null, CommandPropertyChanged);
private static void CommandPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (newValue is ICommand command && bindable is ContentButton contentButton)
{
contentButton._tapGestureRecognizer.Command = command;
}
}
public ICommand Command
{
get => (ICommand)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment