如何在Xamarin Forms中用图像填充按钮? [英] How to fill a button with an image in Xamarin Forms?

查看:517
本文介绍了如何在Xamarin Forms中用图像填充按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Image填充Button.该按钮位于网格内.问题是图像没有完全填满按钮.我也尝试过使用ImageButton控件.

I am trying to fill a Button with an Image. The button resides within a Grid. The problem is that the image is not fully filling the button. I have also tried to use the ImageButton control.

这是我建立自己的U.I的方式:

Here is how I am building my U.I:

<Grid Grid.Row="1" Grid.Column="1" x:Name="VotingGrid" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
  <Grid.RowDefinitions>
    <RowDefinition Height="*" />
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="45*" />
    <ColumnDefinition Width="10*" />
    <ColumnDefinition Width="45*" />
  </Grid.ColumnDefinitions>

  <Button Grid.Row="0" Grid.Column="0" Image="yay.png" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
  <Button Grid.Row="0" Grid.Column="2" Image="meh.png" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
</Grid>

当前看起来像下面的图片,但是我想用图片填充我的按钮.

It currently looks like the image attached below, but I'd like to image to fill my button.

任何帮助将不胜感激!

推荐答案

尝试放置图片而不是按钮.

Try to place an image instead of button.

您可以使用点击手势手势识别器进行点击事件.

You can make use of Tap Gesture Gesture Recognizer to take click events.

尝试这样的事情:

<Image Source="tapped.jpg">
    <Image.GestureRecognizers>
        <TapGestureRecognizer
                Tapped="OnTapGestureRecognizerTapped"  />
  </Image.GestureRecognizers>
</Image>

事件处理程序的代码

void OnTapGestureRecognizerTapped(object sender, EventArgs args) {
    var imageSender = (Image)sender;
    // Do something
    DisplayAlert ("Alert", "Tap gesture recoganised", "OK");
}


引用:添加点击手势手势识别器

这篇关于如何在Xamarin Forms中用图像填充按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆