【发布时间】:2015-11-24 00:43:58
【问题描述】:
我最初实现了此功能,但只是将图像添加到按钮。然后我意识到我可以简单地向图像添加一个点击手势(不使用按钮)。有什么建议是最好的方法,为什么?谢谢。
【问题讨论】:
标签: image button xamarin.forms
我最初实现了此功能,但只是将图像添加到按钮。然后我意识到我可以简单地向图像添加一个点击手势(不使用按钮)。有什么建议是最好的方法,为什么?谢谢。
【问题讨论】:
标签: image button xamarin.forms
我将自己的“OnClick”事件用于 Image :) 和自定义控件:
public class MyImage : Xamarin.Forms.Image
{
public static BindableProperty OnClickProperty =
BindableProperty.Create("OnClick", typeof(Command), typeof(MyImage));
public Command OnClick
{
get { return (Command)GetValue(OnClickProperty); }
set { SetValue(OnClickProperty, value); }
}
public MyImage()
{
GestureRecognizers.Add(new TapGestureRecognizer() {Command = new Command(DisTap)});
}
private void DisTap(object sender)
{
if (OnClick != null)
{
OnClick.Execute(sender);
}
}
}
然后将它与 MVVM 一起使用,例如:
<local:MyImage Source="{Binding Img}" OnClick="{Binding ImgTapCommand}" />
【讨论】:
这取决于你想要达到的视觉效果。
【讨论】:
你可以使用绝对布局,它可以用来将两个元素放在一起,确保按钮是第二个元素。
<AbsoluteLayout>
<Image Source="clock.png" AbsoluteLayout.LayoutBounds="0.2,0.2,35,35" AbsoluteLayout.LayoutFlags="PositionProportional"/>
<Button AbsoluteLayout.LayoutBounds="0.2,0.2,35,35" AbsoluteLayout.LayoutFlags="PositionProportional" BorderColor="Transparent" BackgroundColor="Transparent" Command="{Binding AlertMeCommand}"/>
</AbsoluteLayout>
【讨论】: