Skip to content

Instantly share code, notes, and snippets.

@sukobuto
Created June 2, 2016 11:17
Show Gist options
  • Save sukobuto/1eb1e1413d3952718f1e98604348af80 to your computer and use it in GitHub Desktop.
Save sukobuto/1eb1e1413d3952718f1e98604348af80 to your computer and use it in GitHub Desktop.
Xamarin.Forms ImageButton
using System.Windows.Input;
using Xamarin.Forms;
namespace xxx.Controls
{
class ImageButton : Image
{
public static readonly BindableProperty CommandProperty
= BindableProperty.Create(
nameof(Command), typeof(ICommand), typeof(ImageButton), null,
propertyChanged: (b, o, n) => ((ImageButton)b).Command = (ICommand)n
);
public static readonly BindableProperty CommandParameterProperty
= BindableProperty.Create(
nameof(CommandParameter), typeof(object), typeof(ImageButton), null,
propertyChanged: (b, o, n) => ((ImageButton)b).CommandParameter = n
);
public ICommand Command {
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public object CommandParameter {
get { return GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
public ImageButton()
{
var gr = new TapGestureRecognizer();
gr.Tapped += (s, e) => {
this.Command?.Execute(this.CommandParameter);
};
this.GestureRecognizers.Add(gr);
}
}
}
@sukobuto
Copy link
Author

sukobuto commented Jun 2, 2016

これと組み合わせて使うと便利。
※PCLに埋め込みリソースとして追加した画像を XAML から使う方法 (Markup Extension)
https://gist.github.com/conceptdev/d906c4538a137932fcf6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment